<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>browserchannel.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_browserchannel.js.html">browserchannel.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of the BrowserChannel class.  A BrowserChannel
<a name="line17"></a> * simulates a bidirectional socket over HTTP. It is the basis of the
<a name="line18"></a> * Gmail Chat IM connections to the server.
<a name="line19"></a> *
<a name="line20"></a> * See http://wiki/Main/BrowserChannel
<a name="line21"></a> * This doesn&#39;t yet completely comform to the design document as we&#39;ve done
<a name="line22"></a> * some renaming and cleanup in the design document that hasn&#39;t yet been
<a name="line23"></a> * implemented in the protocol.
<a name="line24"></a> *
<a name="line25"></a> * Typical usage will look like
<a name="line26"></a> *  var handler = [handler object];
<a name="line27"></a> *  var channel = new BrowserChannel(clientVersion);
<a name="line28"></a> *  channel.setHandler(handler);
<a name="line29"></a> *  channel.connect(&#39;channel/test&#39;, &#39;channel/bind&#39;);
<a name="line30"></a> *
<a name="line31"></a> * See goog.net.BrowserChannel.Handler for the handler interface.
<a name="line32"></a> *
<a name="line33"></a> */
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>goog.provide(&#39;goog.net.BrowserChannel&#39;);
<a name="line37"></a>goog.provide(&#39;goog.net.BrowserChannel.Error&#39;);
<a name="line38"></a>goog.provide(&#39;goog.net.BrowserChannel.Event&#39;);
<a name="line39"></a>goog.provide(&#39;goog.net.BrowserChannel.Handler&#39;);
<a name="line40"></a>goog.provide(&#39;goog.net.BrowserChannel.LogSaver&#39;);
<a name="line41"></a>goog.provide(&#39;goog.net.BrowserChannel.QueuedMap&#39;);
<a name="line42"></a>goog.provide(&#39;goog.net.BrowserChannel.ServerReachability&#39;);
<a name="line43"></a>goog.provide(&#39;goog.net.BrowserChannel.ServerReachabilityEvent&#39;);
<a name="line44"></a>goog.provide(&#39;goog.net.BrowserChannel.Stat&#39;);
<a name="line45"></a>goog.provide(&#39;goog.net.BrowserChannel.StatEvent&#39;);
<a name="line46"></a>goog.provide(&#39;goog.net.BrowserChannel.State&#39;);
<a name="line47"></a>goog.provide(&#39;goog.net.BrowserChannel.TimingEvent&#39;);
<a name="line48"></a>
<a name="line49"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line50"></a>goog.require(&#39;goog.array&#39;);
<a name="line51"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line52"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line53"></a>goog.require(&#39;goog.debug.TextFormatter&#39;);
<a name="line54"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line55"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line56"></a>goog.require(&#39;goog.json&#39;);
<a name="line57"></a>goog.require(&#39;goog.json.EvalJsonProcessor&#39;);
<a name="line58"></a>goog.require(&#39;goog.net.BrowserTestChannel&#39;);
<a name="line59"></a>goog.require(&#39;goog.net.ChannelDebug&#39;);
<a name="line60"></a>goog.require(&#39;goog.net.ChannelRequest&#39;);
<a name="line61"></a>goog.require(&#39;goog.net.ChannelRequest.Error&#39;);
<a name="line62"></a>goog.require(&#39;goog.net.XhrIo&#39;);
<a name="line63"></a>goog.require(&#39;goog.net.tmpnetwork&#39;);
<a name="line64"></a>goog.require(&#39;goog.string&#39;);
<a name="line65"></a>goog.require(&#39;goog.structs&#39;);
<a name="line66"></a>goog.require(&#39;goog.structs.CircularBuffer&#39;);
<a name="line67"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Encapsulates the logic for a single BrowserChannel.
<a name="line73"></a> *
<a name="line74"></a> * @param {string=} opt_clientVersion An application-specific version number
<a name="line75"></a> *        that is sent to the server when connected.
<a name="line76"></a> * @param {Array.&lt;string&gt;=} opt_firstTestResults Previously determined results
<a name="line77"></a> *        of the first browser channel test.
<a name="line78"></a> * @constructor
<a name="line79"></a> */
<a name="line80"></a>  goog.net.BrowserChannel = function(opt_clientVersion, opt_firstTestResults) {
<a name="line81"></a>  /**
<a name="line82"></a>   * The application specific version that is passed to the server.
<a name="line83"></a>   * @type {?string}
<a name="line84"></a>   * @private
<a name="line85"></a>   */
<a name="line86"></a>  this.clientVersion_ = opt_clientVersion || null;
<a name="line87"></a>
<a name="line88"></a>  /**
<a name="line89"></a>   * The current state of the BrowserChannel. It should be one of the
<a name="line90"></a>   * goog.net.BrowserChannel.State constants.
<a name="line91"></a>   * @type {!goog.net.BrowserChannel.State}
<a name="line92"></a>   * @private
<a name="line93"></a>   */
<a name="line94"></a>  this.state_ = goog.net.BrowserChannel.State.INIT;
<a name="line95"></a>
<a name="line96"></a>  /**
<a name="line97"></a>   * An array of queued maps that need to be sent to the server.
<a name="line98"></a>   * @type {Array.&lt;goog.net.BrowserChannel.QueuedMap&gt;}
<a name="line99"></a>   * @private
<a name="line100"></a>   */
<a name="line101"></a>  this.outgoingMaps_ = [];
<a name="line102"></a>
<a name="line103"></a>  /**
<a name="line104"></a>   * An array of dequeued maps that we have either received a non-successful
<a name="line105"></a>   * response for, or no response at all, and which therefore may or may not
<a name="line106"></a>   * have been received by the server.
<a name="line107"></a>   * @type {Array.&lt;goog.net.BrowserChannel.QueuedMap&gt;}
<a name="line108"></a>   * @private
<a name="line109"></a>   */
<a name="line110"></a>  this.pendingMaps_ = [];
<a name="line111"></a>
<a name="line112"></a>  /**
<a name="line113"></a>   * The channel debug used for browserchannel logging
<a name="line114"></a>   * @type {!goog.net.ChannelDebug}
<a name="line115"></a>   * @private
<a name="line116"></a>   */
<a name="line117"></a>  this.channelDebug_ = new goog.net.ChannelDebug();
<a name="line118"></a>
<a name="line119"></a>  /**
<a name="line120"></a>   * Parser for a response payload. Defaults to use
<a name="line121"></a>   * {@code goog.json.unsafeParse}. The parser should return an array.
<a name="line122"></a>   * @type {!goog.string.Parser}
<a name="line123"></a>   * @private
<a name="line124"></a>   */
<a name="line125"></a>  this.parser_ = new goog.json.EvalJsonProcessor(null, true);
<a name="line126"></a>
<a name="line127"></a>  /**
<a name="line128"></a>   * An array of results for the first browser channel test call.
<a name="line129"></a>   * @type {Array.&lt;string&gt;}
<a name="line130"></a>   * @private
<a name="line131"></a>   */
<a name="line132"></a>  this.firstTestResults_ = opt_firstTestResults || null;
<a name="line133"></a>};
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Simple container class for a (mapId, map) pair.
<a name="line139"></a> * @param {number} mapId The id for this map.
<a name="line140"></a> * @param {Object|goog.structs.Map} map The map itself.
<a name="line141"></a> * @param {Object=} opt_context The context associated with the map.
<a name="line142"></a> * @constructor
<a name="line143"></a> */
<a name="line144"></a>goog.net.BrowserChannel.QueuedMap = function(mapId, map, opt_context) {
<a name="line145"></a>  /**
<a name="line146"></a>   * The id for this map.
<a name="line147"></a>   * @type {number}
<a name="line148"></a>   */
<a name="line149"></a>  this.mapId = mapId;
<a name="line150"></a>
<a name="line151"></a>  /**
<a name="line152"></a>   * The map itself.
<a name="line153"></a>   * @type {Object|goog.structs.Map}
<a name="line154"></a>   */
<a name="line155"></a>  this.map = map;
<a name="line156"></a>
<a name="line157"></a>  /**
<a name="line158"></a>   * The context for the map.
<a name="line159"></a>   * @type {Object}
<a name="line160"></a>   */
<a name="line161"></a>  this.context = opt_context || null;
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Extra HTTP headers to add to all the requests sent to the server.
<a name="line167"></a> * @type {Object}
<a name="line168"></a> * @private
<a name="line169"></a> */
<a name="line170"></a>goog.net.BrowserChannel.prototype.extraHeaders_ = null;
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/**
<a name="line174"></a> * Extra parameters to add to all the requests sent to the server.
<a name="line175"></a> * @type {Object}
<a name="line176"></a> * @private
<a name="line177"></a> */
<a name="line178"></a>goog.net.BrowserChannel.prototype.extraParams_ = null;
<a name="line179"></a>
<a name="line180"></a>
<a name="line181"></a>/**
<a name="line182"></a> * The current ChannelRequest object for the forwardchannel.
<a name="line183"></a> * @type {goog.net.ChannelRequest?}
<a name="line184"></a> * @private
<a name="line185"></a> */
<a name="line186"></a>goog.net.BrowserChannel.prototype.forwardChannelRequest_ = null;
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * The ChannelRequest object for the backchannel.
<a name="line191"></a> * @type {goog.net.ChannelRequest?}
<a name="line192"></a> * @private
<a name="line193"></a> */
<a name="line194"></a>goog.net.BrowserChannel.prototype.backChannelRequest_ = null;
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * The relative path (in the context of the the page hosting the browser
<a name="line199"></a> * channel) for making requests to the server.
<a name="line200"></a> * @type {?string}
<a name="line201"></a> * @private
<a name="line202"></a> */
<a name="line203"></a>goog.net.BrowserChannel.prototype.path_ = null;
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * The absolute URI for the forwardchannel request.
<a name="line208"></a> * @type {goog.Uri}
<a name="line209"></a> * @private
<a name="line210"></a> */
<a name="line211"></a>goog.net.BrowserChannel.prototype.forwardChannelUri_ = null;
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/**
<a name="line215"></a> * The absolute URI for the backchannel request.
<a name="line216"></a> * @type {goog.Uri}
<a name="line217"></a> * @private
<a name="line218"></a> */
<a name="line219"></a>goog.net.BrowserChannel.prototype.backChannelUri_ = null;
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * A subdomain prefix for using a subdomain in IE for the backchannel
<a name="line224"></a> * requests.
<a name="line225"></a> * @type {?string}
<a name="line226"></a> * @private
<a name="line227"></a> */
<a name="line228"></a>goog.net.BrowserChannel.prototype.hostPrefix_ = null;
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Whether we allow the use of a subdomain in IE for the backchannel requests.
<a name="line233"></a> * @private
<a name="line234"></a> */
<a name="line235"></a>goog.net.BrowserChannel.prototype.allowHostPrefix_ = true;
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * The next id to use for the RID (request identifier) parameter. This
<a name="line240"></a> * identifier uniquely identifies the forward channel request.
<a name="line241"></a> * @type {number}
<a name="line242"></a> * @private
<a name="line243"></a> */
<a name="line244"></a>goog.net.BrowserChannel.prototype.nextRid_ = 0;
<a name="line245"></a>
<a name="line246"></a>
<a name="line247"></a>/**
<a name="line248"></a> * The id to use for the next outgoing map. This identifier uniquely
<a name="line249"></a> * identifies a sent map.
<a name="line250"></a> * @type {number}
<a name="line251"></a> * @private
<a name="line252"></a> */
<a name="line253"></a>goog.net.BrowserChannel.prototype.nextMapId_ = 0;
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * Whether to fail forward-channel requests after one try, or after a few tries.
<a name="line258"></a> * @type {boolean}
<a name="line259"></a> * @private
<a name="line260"></a> */
<a name="line261"></a>goog.net.BrowserChannel.prototype.failFast_ = false;
<a name="line262"></a>
<a name="line263"></a>
<a name="line264"></a>/**
<a name="line265"></a> * The handler that receive callbacks for state changes and data.
<a name="line266"></a> * @type {goog.net.BrowserChannel.Handler}
<a name="line267"></a> * @private
<a name="line268"></a> */
<a name="line269"></a>goog.net.BrowserChannel.prototype.handler_ = null;
<a name="line270"></a>
<a name="line271"></a>
<a name="line272"></a>/**
<a name="line273"></a> * Timer identifier for asynchronously making a forward channel request.
<a name="line274"></a> * @type {?number}
<a name="line275"></a> * @private
<a name="line276"></a> */
<a name="line277"></a>goog.net.BrowserChannel.prototype.forwardChannelTimerId_ = null;
<a name="line278"></a>
<a name="line279"></a>
<a name="line280"></a>/**
<a name="line281"></a> * Timer identifier for asynchronously making a back channel request.
<a name="line282"></a> * @type {?number}
<a name="line283"></a> * @private
<a name="line284"></a> */
<a name="line285"></a>goog.net.BrowserChannel.prototype.backChannelTimerId_ = null;
<a name="line286"></a>
<a name="line287"></a>
<a name="line288"></a>/**
<a name="line289"></a> * Timer identifier for the timer that waits for us to retry the backchannel in
<a name="line290"></a> * the case where it is dead and no longer receiving data.
<a name="line291"></a> * @type {?number}
<a name="line292"></a> * @private
<a name="line293"></a> */
<a name="line294"></a>goog.net.BrowserChannel.prototype.deadBackChannelTimerId_ = null;
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * The BrowserTestChannel object which encapsulates the logic for determining
<a name="line299"></a> * interesting network conditions about the client.
<a name="line300"></a> * @type {goog.net.BrowserTestChannel?}
<a name="line301"></a> * @private
<a name="line302"></a> */
<a name="line303"></a>goog.net.BrowserChannel.prototype.connectionTest_ = null;
<a name="line304"></a>
<a name="line305"></a>
<a name="line306"></a>/**
<a name="line307"></a> * Whether the client&#39;s network conditions can support chunked responses.
<a name="line308"></a> * @type {?boolean}
<a name="line309"></a> * @private
<a name="line310"></a> */
<a name="line311"></a>goog.net.BrowserChannel.prototype.useChunked_ = null;
<a name="line312"></a>
<a name="line313"></a>
<a name="line314"></a>/**
<a name="line315"></a> * Whether chunked mode is allowed. In certain debugging situations, it&#39;s
<a name="line316"></a> * useful to disable this.
<a name="line317"></a> * @private
<a name="line318"></a> */
<a name="line319"></a>goog.net.BrowserChannel.prototype.allowChunkedMode_ = true;
<a name="line320"></a>
<a name="line321"></a>
<a name="line322"></a>/**
<a name="line323"></a> * The array identifier of the last array received from the server for the
<a name="line324"></a> * backchannel request.
<a name="line325"></a> * @type {number}
<a name="line326"></a> * @private
<a name="line327"></a> */
<a name="line328"></a>goog.net.BrowserChannel.prototype.lastArrayId_ = -1;
<a name="line329"></a>
<a name="line330"></a>
<a name="line331"></a>/**
<a name="line332"></a> * The array identifier of the last array sent by the server that we know about.
<a name="line333"></a> * @type {number}
<a name="line334"></a> * @private
<a name="line335"></a> */
<a name="line336"></a>goog.net.BrowserChannel.prototype.lastPostResponseArrayId_ = -1;
<a name="line337"></a>
<a name="line338"></a>
<a name="line339"></a>/**
<a name="line340"></a> * The last status code received.
<a name="line341"></a> * @type {number}
<a name="line342"></a> * @private
<a name="line343"></a> */
<a name="line344"></a>goog.net.BrowserChannel.prototype.lastStatusCode_ = -1;
<a name="line345"></a>
<a name="line346"></a>
<a name="line347"></a>/**
<a name="line348"></a> * Number of times we have retried the current forward channel request.
<a name="line349"></a> * @type {number}
<a name="line350"></a> * @private
<a name="line351"></a> */
<a name="line352"></a>goog.net.BrowserChannel.prototype.forwardChannelRetryCount_ = 0;
<a name="line353"></a>
<a name="line354"></a>
<a name="line355"></a>/**
<a name="line356"></a> * Number of times it a row that we have retried the current back channel
<a name="line357"></a> * request and received no data.
<a name="line358"></a> * @type {number}
<a name="line359"></a> * @private
<a name="line360"></a> */
<a name="line361"></a>goog.net.BrowserChannel.prototype.backChannelRetryCount_ = 0;
<a name="line362"></a>
<a name="line363"></a>
<a name="line364"></a>/**
<a name="line365"></a> * The attempt id for the current back channel request. Starts at 1 and
<a name="line366"></a> * increments for each reconnect. The server uses this to log if our connection
<a name="line367"></a> * is flaky or not.
<a name="line368"></a> * @type {number}
<a name="line369"></a> * @private
<a name="line370"></a> */
<a name="line371"></a>goog.net.BrowserChannel.prototype.backChannelAttemptId_;
<a name="line372"></a>
<a name="line373"></a>
<a name="line374"></a>/**
<a name="line375"></a> * The base part of the time before firing next retry request. Default is 5
<a name="line376"></a> * seconds. Note that a random delay is added (see {@link retryDelaySeedMs_})
<a name="line377"></a> * for all retries, and linear backoff is applied to the sum for subsequent
<a name="line378"></a> * retries.
<a name="line379"></a> * @type {number}
<a name="line380"></a> * @private
<a name="line381"></a> */
<a name="line382"></a>goog.net.BrowserChannel.prototype.baseRetryDelayMs_ = 5 * 1000;
<a name="line383"></a>
<a name="line384"></a>
<a name="line385"></a>/**
<a name="line386"></a> * A random time between 0 and this number of MS is added to the
<a name="line387"></a> * {@link baseRetryDelayMs_}. Default is 10 seconds.
<a name="line388"></a> * @type {number}
<a name="line389"></a> * @private
<a name="line390"></a> */
<a name="line391"></a>goog.net.BrowserChannel.prototype.retryDelaySeedMs_ = 10 * 1000;
<a name="line392"></a>
<a name="line393"></a>
<a name="line394"></a>/**
<a name="line395"></a> * Maximum number of attempts to connect to the server for forward channel
<a name="line396"></a> * requests. Defaults to 2.
<a name="line397"></a> * @type {number}
<a name="line398"></a> * @private
<a name="line399"></a> */
<a name="line400"></a>goog.net.BrowserChannel.prototype.forwardChannelMaxRetries_ = 2;
<a name="line401"></a>
<a name="line402"></a>
<a name="line403"></a>/**
<a name="line404"></a> * The timeout in milliseconds for a forward channel request. Defaults to 20
<a name="line405"></a> * seconds. Note that part of this timeout can be randomized.
<a name="line406"></a> * @type {number}
<a name="line407"></a> * @private
<a name="line408"></a> */
<a name="line409"></a>goog.net.BrowserChannel.prototype.forwardChannelRequestTimeoutMs_ = 20 * 1000;
<a name="line410"></a>
<a name="line411"></a>
<a name="line412"></a>/**
<a name="line413"></a> * A throttle time in ms for readystatechange events for the backchannel.
<a name="line414"></a> * Useful for throttling when ready state is INTERACTIVE (partial data).
<a name="line415"></a> *
<a name="line416"></a> * This throttle is useful if the server sends large data chunks down the
<a name="line417"></a> * backchannel.  It prevents examining XHR partial data on every
<a name="line418"></a> * readystate change event.  This is useful because large chunks can
<a name="line419"></a> * trigger hundreds of readystatechange events, each of which takes ~5ms
<a name="line420"></a> * or so to handle, in turn making the UI unresponsive for a significant period.
<a name="line421"></a> *
<a name="line422"></a> * If set to zero no throttle is used.
<a name="line423"></a> * @type {number}
<a name="line424"></a> * @private
<a name="line425"></a> */
<a name="line426"></a>goog.net.BrowserChannel.prototype.readyStateChangeThrottleMs_ = 0;
<a name="line427"></a>
<a name="line428"></a>
<a name="line429"></a>/**
<a name="line430"></a> * Whether cross origin requests are supported for the browser channel.
<a name="line431"></a> *
<a name="line432"></a> * See {@link goog.net.XhrIo#setWithCredentials}.
<a name="line433"></a> * @type {boolean}
<a name="line434"></a> * @private
<a name="line435"></a> */
<a name="line436"></a>goog.net.BrowserChannel.prototype.supportsCrossDomainXhrs_ = false;
<a name="line437"></a>
<a name="line438"></a>
<a name="line439"></a>/**
<a name="line440"></a> * The latest protocol version that this class supports. We request this version
<a name="line441"></a> * from the server when opening the connection. Should match
<a name="line442"></a> * com.google.net.browserchannel.BrowserChannel.LATEST_CHANNEL_VERSION.
<a name="line443"></a> * @type {number}
<a name="line444"></a> */
<a name="line445"></a>goog.net.BrowserChannel.LATEST_CHANNEL_VERSION = 8;
<a name="line446"></a>
<a name="line447"></a>
<a name="line448"></a>/**
<a name="line449"></a> * The channel version that we negotiated with the server for this session.
<a name="line450"></a> * Starts out as the version we request, and then is changed to the negotiated
<a name="line451"></a> * version after the initial open.
<a name="line452"></a> * @type {number}
<a name="line453"></a> * @private
<a name="line454"></a> */
<a name="line455"></a>goog.net.BrowserChannel.prototype.channelVersion_ =
<a name="line456"></a>    goog.net.BrowserChannel.LATEST_CHANNEL_VERSION;
<a name="line457"></a>
<a name="line458"></a>
<a name="line459"></a>/**
<a name="line460"></a> * Enum type for the browser channel state machine.
<a name="line461"></a> * @enum {number}
<a name="line462"></a> */
<a name="line463"></a>goog.net.BrowserChannel.State = {
<a name="line464"></a>  /** The channel is closed. */
<a name="line465"></a>  CLOSED: 0,
<a name="line466"></a>
<a name="line467"></a>  /** The channel has been initialized but hasn&#39;t yet initiated a connection. */
<a name="line468"></a>  INIT: 1,
<a name="line469"></a>
<a name="line470"></a>  /** The channel is in the process of opening a connection to the server. */
<a name="line471"></a>  OPENING: 2,
<a name="line472"></a>
<a name="line473"></a>  /** The channel is open. */
<a name="line474"></a>  OPENED: 3
<a name="line475"></a>};
<a name="line476"></a>
<a name="line477"></a>
<a name="line478"></a>/**
<a name="line479"></a> * The timeout in milliseconds for a forward channel request.
<a name="line480"></a> * @type {number}
<a name="line481"></a> */
<a name="line482"></a>goog.net.BrowserChannel.FORWARD_CHANNEL_RETRY_TIMEOUT = 20 * 1000;
<a name="line483"></a>
<a name="line484"></a>
<a name="line485"></a>/**
<a name="line486"></a> * Maximum number of attempts to connect to the server for back channel
<a name="line487"></a> * requests.
<a name="line488"></a> * @type {number}
<a name="line489"></a> */
<a name="line490"></a>goog.net.BrowserChannel.BACK_CHANNEL_MAX_RETRIES = 3;
<a name="line491"></a>
<a name="line492"></a>
<a name="line493"></a>/**
<a name="line494"></a> * A number in MS of how long we guess the maxmium amount of time a round trip
<a name="line495"></a> * to the server should take. In the future this could be substituted with a
<a name="line496"></a> * real measurement of the RTT.
<a name="line497"></a> * @type {number}
<a name="line498"></a> */
<a name="line499"></a>goog.net.BrowserChannel.RTT_ESTIMATE = 3 * 1000;
<a name="line500"></a>
<a name="line501"></a>
<a name="line502"></a>/**
<a name="line503"></a> * When retrying for an inactive channel, we will multiply the total delay by
<a name="line504"></a> * this number.
<a name="line505"></a> * @type {number}
<a name="line506"></a> */
<a name="line507"></a>goog.net.BrowserChannel.INACTIVE_CHANNEL_RETRY_FACTOR = 2;
<a name="line508"></a>
<a name="line509"></a>
<a name="line510"></a>/**
<a name="line511"></a> * Enum type for identifying a BrowserChannel error.
<a name="line512"></a> * @enum {number}
<a name="line513"></a> */
<a name="line514"></a>goog.net.BrowserChannel.Error = {
<a name="line515"></a>  /** Value that indicates no error has occurred. */
<a name="line516"></a>  OK: 0,
<a name="line517"></a>
<a name="line518"></a>  /** An error due to a request failing. */
<a name="line519"></a>  REQUEST_FAILED: 2,
<a name="line520"></a>
<a name="line521"></a>  /** An error due to the user being logged out. */
<a name="line522"></a>  LOGGED_OUT: 4,
<a name="line523"></a>
<a name="line524"></a>  /** An error due to server response which contains no data. */
<a name="line525"></a>  NO_DATA: 5,
<a name="line526"></a>
<a name="line527"></a>  /** An error due to a server response indicating an unknown session id */
<a name="line528"></a>  UNKNOWN_SESSION_ID: 6,
<a name="line529"></a>
<a name="line530"></a>  /** An error due to a server response requesting to stop the channel. */
<a name="line531"></a>  STOP: 7,
<a name="line532"></a>
<a name="line533"></a>  /** A general network error. */
<a name="line534"></a>  NETWORK: 8,
<a name="line535"></a>
<a name="line536"></a>  /** An error due to the channel being blocked by a network administrator. */
<a name="line537"></a>  BLOCKED: 9,
<a name="line538"></a>
<a name="line539"></a>  /** An error due to bad data being returned from the server. */
<a name="line540"></a>  BAD_DATA: 10,
<a name="line541"></a>
<a name="line542"></a>  /** An error due to a response that doesn&#39;t start with the magic cookie. */
<a name="line543"></a>  BAD_RESPONSE: 11,
<a name="line544"></a>
<a name="line545"></a>  /** ActiveX is blocked by the machine&#39;s admin settings. */
<a name="line546"></a>  ACTIVE_X_BLOCKED: 12
<a name="line547"></a>};
<a name="line548"></a>
<a name="line549"></a>
<a name="line550"></a>/**
<a name="line551"></a> * Internal enum type for the two browser channel channel types.
<a name="line552"></a> * @enum {number}
<a name="line553"></a> * @private
<a name="line554"></a> */
<a name="line555"></a>goog.net.BrowserChannel.ChannelType_ = {
<a name="line556"></a>  FORWARD_CHANNEL: 1,
<a name="line557"></a>
<a name="line558"></a>  BACK_CHANNEL: 2
<a name="line559"></a>};
<a name="line560"></a>
<a name="line561"></a>
<a name="line562"></a>/**
<a name="line563"></a> * The maximum number of maps that can be sent in one POST. Should match
<a name="line564"></a> * com.google.net.browserchannel.BrowserChannel.MAX_MAPS_PER_REQUEST.
<a name="line565"></a> * @type {number}
<a name="line566"></a> * @private
<a name="line567"></a> */
<a name="line568"></a>goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_ = 1000;
<a name="line569"></a>
<a name="line570"></a>
<a name="line571"></a>/**
<a name="line572"></a> * Singleton event target for firing stat events
<a name="line573"></a> * @type {goog.events.EventTarget}
<a name="line574"></a> * @private
<a name="line575"></a> */
<a name="line576"></a>goog.net.BrowserChannel.statEventTarget_ = new goog.events.EventTarget();
<a name="line577"></a>
<a name="line578"></a>
<a name="line579"></a>/**
<a name="line580"></a> * Events fired by BrowserChannel and associated objects
<a name="line581"></a> * @type {Object}
<a name="line582"></a> */
<a name="line583"></a>goog.net.BrowserChannel.Event = {};
<a name="line584"></a>
<a name="line585"></a>
<a name="line586"></a>/**
<a name="line587"></a> * Stat Event that fires when things of interest happen that may be useful for
<a name="line588"></a> * applications to know about for stats or debugging purposes. This event fires
<a name="line589"></a> * on the EventTarget returned by getStatEventTarget.
<a name="line590"></a> */
<a name="line591"></a>goog.net.BrowserChannel.Event.STAT_EVENT = &#39;statevent&#39;;
<a name="line592"></a>
<a name="line593"></a>
<a name="line594"></a>
<a name="line595"></a>/**
<a name="line596"></a> * Event class for goog.net.BrowserChannel.Event.STAT_EVENT
<a name="line597"></a> *
<a name="line598"></a> * @param {goog.events.EventTarget} eventTarget The stat event target for
<a name="line599"></a>       the browser channel.
<a name="line600"></a> * @param {goog.net.BrowserChannel.Stat} stat The stat.
<a name="line601"></a> * @constructor
<a name="line602"></a> * @extends {goog.events.Event}
<a name="line603"></a> */
<a name="line604"></a>goog.net.BrowserChannel.StatEvent = function(eventTarget, stat) {
<a name="line605"></a>  goog.events.Event.call(this, goog.net.BrowserChannel.Event.STAT_EVENT,
<a name="line606"></a>      eventTarget);
<a name="line607"></a>
<a name="line608"></a>  /**
<a name="line609"></a>   * The stat
<a name="line610"></a>   * @type {goog.net.BrowserChannel.Stat}
<a name="line611"></a>   */
<a name="line612"></a>  this.stat = stat;
<a name="line613"></a>
<a name="line614"></a>};
<a name="line615"></a>goog.inherits(goog.net.BrowserChannel.StatEvent, goog.events.Event);
<a name="line616"></a>
<a name="line617"></a>
<a name="line618"></a>/**
<a name="line619"></a> * An event that fires when POST requests complete successfully, indicating
<a name="line620"></a> * the size of the POST and the round trip time.
<a name="line621"></a> * This event fires on the EventTarget returned by getStatEventTarget.
<a name="line622"></a> */
<a name="line623"></a>goog.net.BrowserChannel.Event.TIMING_EVENT = &#39;timingevent&#39;;
<a name="line624"></a>
<a name="line625"></a>
<a name="line626"></a>
<a name="line627"></a>/**
<a name="line628"></a> * Event class for goog.net.BrowserChannel.Event.TIMING_EVENT
<a name="line629"></a> *
<a name="line630"></a> * @param {goog.events.EventTarget} target The stat event target for
<a name="line631"></a>       the browser channel.
<a name="line632"></a> * @param {number} size The number of characters in the POST data.
<a name="line633"></a> * @param {number} rtt The total round trip time from POST to response in MS.
<a name="line634"></a> * @param {number} retries The number of times the POST had to be retried.
<a name="line635"></a> * @constructor
<a name="line636"></a> * @extends {goog.events.Event}
<a name="line637"></a> */
<a name="line638"></a>goog.net.BrowserChannel.TimingEvent = function(target, size, rtt, retries) {
<a name="line639"></a>  goog.events.Event.call(this, goog.net.BrowserChannel.Event.TIMING_EVENT,
<a name="line640"></a>      target);
<a name="line641"></a>
<a name="line642"></a>  /**
<a name="line643"></a>   * @type {number}
<a name="line644"></a>   */
<a name="line645"></a>  this.size = size;
<a name="line646"></a>
<a name="line647"></a>  /**
<a name="line648"></a>   * @type {number}
<a name="line649"></a>   */
<a name="line650"></a>  this.rtt = rtt;
<a name="line651"></a>
<a name="line652"></a>  /**
<a name="line653"></a>   * @type {number}
<a name="line654"></a>   */
<a name="line655"></a>  this.retries = retries;
<a name="line656"></a>
<a name="line657"></a>};
<a name="line658"></a>goog.inherits(goog.net.BrowserChannel.TimingEvent, goog.events.Event);
<a name="line659"></a>
<a name="line660"></a>
<a name="line661"></a>/**
<a name="line662"></a> * The type of event that occurs every time some information about how reachable
<a name="line663"></a> * the server is is discovered.
<a name="line664"></a> */
<a name="line665"></a>goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT =
<a name="line666"></a>    &#39;serverreachability&#39;;
<a name="line667"></a>
<a name="line668"></a>
<a name="line669"></a>/**
<a name="line670"></a> * Types of events which reveal information about the reachability of the
<a name="line671"></a> * server.
<a name="line672"></a> * @enum {number}
<a name="line673"></a> */
<a name="line674"></a>goog.net.BrowserChannel.ServerReachability = {
<a name="line675"></a>  REQUEST_MADE: 1,
<a name="line676"></a>  REQUEST_SUCCEEDED: 2,
<a name="line677"></a>  REQUEST_FAILED: 3,
<a name="line678"></a>  BACK_CHANNEL_ACTIVITY: 4
<a name="line679"></a>};
<a name="line680"></a>
<a name="line681"></a>
<a name="line682"></a>
<a name="line683"></a>/**
<a name="line684"></a> * Event class for goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT.
<a name="line685"></a> *
<a name="line686"></a> * @param {goog.events.EventTarget} target The stat event target for
<a name="line687"></a>       the browser channel.
<a name="line688"></a> * @param {goog.net.BrowserChannel.ServerReachability} reachabilityType The
<a name="line689"></a> *     reachability event type.
<a name="line690"></a> * @constructor
<a name="line691"></a> * @extends {goog.events.Event}
<a name="line692"></a> */
<a name="line693"></a>goog.net.BrowserChannel.ServerReachabilityEvent = function(target,
<a name="line694"></a>    reachabilityType) {
<a name="line695"></a>  goog.events.Event.call(this,
<a name="line696"></a>      goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT, target);
<a name="line697"></a>
<a name="line698"></a>  /**
<a name="line699"></a>   * @type {goog.net.BrowserChannel.ServerReachability}
<a name="line700"></a>   */
<a name="line701"></a>  this.reachabilityType = reachabilityType;
<a name="line702"></a>};
<a name="line703"></a>goog.inherits(goog.net.BrowserChannel.ServerReachabilityEvent,
<a name="line704"></a>    goog.events.Event);
<a name="line705"></a>
<a name="line706"></a>
<a name="line707"></a>/**
<a name="line708"></a> * Enum that identifies events for statistics that are interesting to track.
<a name="line709"></a> * TODO(user) - Change name not to use Event or use EventTarget
<a name="line710"></a> * @enum {number}
<a name="line711"></a> */
<a name="line712"></a>goog.net.BrowserChannel.Stat = {
<a name="line713"></a>  /** Event indicating a new connection attempt. */
<a name="line714"></a>  CONNECT_ATTEMPT: 0,
<a name="line715"></a>
<a name="line716"></a>  /** Event indicating a connection error due to a general network problem. */
<a name="line717"></a>  ERROR_NETWORK: 1,
<a name="line718"></a>
<a name="line719"></a>  /**
<a name="line720"></a>   * Event indicating a connection error that isn&#39;t due to a general network
<a name="line721"></a>   * problem.
<a name="line722"></a>   */
<a name="line723"></a>  ERROR_OTHER: 2,
<a name="line724"></a>
<a name="line725"></a>  /** Event indicating the start of test stage one. */
<a name="line726"></a>  TEST_STAGE_ONE_START: 3,
<a name="line727"></a>
<a name="line728"></a>
<a name="line729"></a>  /** Event indicating the channel is blocked by a network administrator. */
<a name="line730"></a>  CHANNEL_BLOCKED: 4,
<a name="line731"></a>
<a name="line732"></a>  /** Event indicating the start of test stage two. */
<a name="line733"></a>  TEST_STAGE_TWO_START: 5,
<a name="line734"></a>
<a name="line735"></a>  /** Event indicating the first piece of test data was received. */
<a name="line736"></a>  TEST_STAGE_TWO_DATA_ONE: 6,
<a name="line737"></a>
<a name="line738"></a>  /**
<a name="line739"></a>   * Event indicating that the second piece of test data was received and it was
<a name="line740"></a>   * recieved separately from the first.
<a name="line741"></a>   */
<a name="line742"></a>  TEST_STAGE_TWO_DATA_TWO: 7,
<a name="line743"></a>
<a name="line744"></a>  /** Event indicating both pieces of test data were received simultaneously. */
<a name="line745"></a>  TEST_STAGE_TWO_DATA_BOTH: 8,
<a name="line746"></a>
<a name="line747"></a>  /** Event indicating stage one of the test request failed. */
<a name="line748"></a>  TEST_STAGE_ONE_FAILED: 9,
<a name="line749"></a>
<a name="line750"></a>  /** Event indicating stage two of the test request failed. */
<a name="line751"></a>  TEST_STAGE_TWO_FAILED: 10,
<a name="line752"></a>
<a name="line753"></a>  /**
<a name="line754"></a>   * Event indicating that a buffering proxy is likely between the client and
<a name="line755"></a>   * the server.
<a name="line756"></a>   */
<a name="line757"></a>  PROXY: 11,
<a name="line758"></a>
<a name="line759"></a>  /**
<a name="line760"></a>   * Event indicating that no buffering proxy is likely between the client and
<a name="line761"></a>   * the server.
<a name="line762"></a>   */
<a name="line763"></a>  NOPROXY: 12,
<a name="line764"></a>
<a name="line765"></a>  /** Event indicating an unknown SID error. */
<a name="line766"></a>  REQUEST_UNKNOWN_SESSION_ID: 13,
<a name="line767"></a>
<a name="line768"></a>  /** Event indicating a bad status code was received. */
<a name="line769"></a>  REQUEST_BAD_STATUS: 14,
<a name="line770"></a>
<a name="line771"></a>  /** Event indicating incomplete data was received */
<a name="line772"></a>  REQUEST_INCOMPLETE_DATA: 15,
<a name="line773"></a>
<a name="line774"></a>  /** Event indicating bad data was received */
<a name="line775"></a>  REQUEST_BAD_DATA: 16,
<a name="line776"></a>
<a name="line777"></a>  /** Event indicating no data was received when data was expected. */
<a name="line778"></a>  REQUEST_NO_DATA: 17,
<a name="line779"></a>
<a name="line780"></a>  /** Event indicating a request timeout. */
<a name="line781"></a>  REQUEST_TIMEOUT: 18,
<a name="line782"></a>
<a name="line783"></a>  /**
<a name="line784"></a>   * Event indicating that the server never received our hanging GET and so it
<a name="line785"></a>   * is being retried.
<a name="line786"></a>   */
<a name="line787"></a>  BACKCHANNEL_MISSING: 19,
<a name="line788"></a>
<a name="line789"></a>  /**
<a name="line790"></a>   * Event indicating that we have determined that our hanging GET is not
<a name="line791"></a>   * receiving data when it should be. Thus it is dead dead and will be retried.
<a name="line792"></a>   */
<a name="line793"></a>  BACKCHANNEL_DEAD: 20,
<a name="line794"></a>
<a name="line795"></a>  /**
<a name="line796"></a>   * The browser declared itself offline during the lifetime of a request, or
<a name="line797"></a>   * was offline when a request was initially made.
<a name="line798"></a>   */
<a name="line799"></a>  BROWSER_OFFLINE: 21,
<a name="line800"></a>
<a name="line801"></a>  /** ActiveX is blocked by the machine&#39;s admin settings. */
<a name="line802"></a>  ACTIVE_X_BLOCKED: 22
<a name="line803"></a>};
<a name="line804"></a>
<a name="line805"></a>
<a name="line806"></a>/**
<a name="line807"></a> * The normal response for forward channel requests.
<a name="line808"></a> * Used only before version 8 of the protocol.
<a name="line809"></a> * @type {string}
<a name="line810"></a> */
<a name="line811"></a>goog.net.BrowserChannel.MAGIC_RESPONSE_COOKIE = &#39;y2f%&#39;;
<a name="line812"></a>
<a name="line813"></a>
<a name="line814"></a>/**
<a name="line815"></a> * A guess at a cutoff at which to no longer assume the backchannel is dead
<a name="line816"></a> * when we are slow to receive data. Number in bytes.
<a name="line817"></a> *
<a name="line818"></a> * Assumption: The worst bandwidth we work on is 50 kilobits/sec
<a name="line819"></a> * 50kbits/sec * (1 byte / 8 bits) * 6 sec dead backchannel timeout
<a name="line820"></a> * @type {number}
<a name="line821"></a> */
<a name="line822"></a>goog.net.BrowserChannel.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF = 37500;
<a name="line823"></a>
<a name="line824"></a>
<a name="line825"></a>/**
<a name="line826"></a> * Returns the browserchannel logger.
<a name="line827"></a> *
<a name="line828"></a> * @return {goog.net.ChannelDebug} The channel debug object.
<a name="line829"></a> */
<a name="line830"></a>goog.net.BrowserChannel.prototype.getChannelDebug = function() {
<a name="line831"></a>  return this.channelDebug_;
<a name="line832"></a>};
<a name="line833"></a>
<a name="line834"></a>
<a name="line835"></a>/**
<a name="line836"></a> * Set the browserchannel logger.
<a name="line837"></a> * TODO(user): Add interface for channel loggers or remove this function.
<a name="line838"></a> *
<a name="line839"></a> * @param {goog.net.ChannelDebug} channelDebug The channel debug object.
<a name="line840"></a> */
<a name="line841"></a>goog.net.BrowserChannel.prototype.setChannelDebug = function(
<a name="line842"></a>    channelDebug) {
<a name="line843"></a>  if (goog.isDefAndNotNull(channelDebug)) {
<a name="line844"></a>    this.channelDebug_ = channelDebug;
<a name="line845"></a>  }
<a name="line846"></a>};
<a name="line847"></a>
<a name="line848"></a>
<a name="line849"></a>/**
<a name="line850"></a> * Allows the application to set an execution hooks for when BrowserChannel
<a name="line851"></a> * starts processing requests. This is useful to track timing or logging
<a name="line852"></a> * special information. The function takes no parameters and return void.
<a name="line853"></a> * @param {Function} startHook  The function for the start hook.
<a name="line854"></a> */
<a name="line855"></a>goog.net.BrowserChannel.setStartThreadExecutionHook = function(startHook) {
<a name="line856"></a>  goog.net.BrowserChannel.startExecutionHook_ = startHook;
<a name="line857"></a>};
<a name="line858"></a>
<a name="line859"></a>
<a name="line860"></a>/**
<a name="line861"></a> * Allows the application to set an execution hooks for when BrowserChannel
<a name="line862"></a> * stops processing requests. This is useful to track timing or logging
<a name="line863"></a> * special information. The function takes no parameters and return void.
<a name="line864"></a> * @param {Function} endHook  The function for the end hook.
<a name="line865"></a> */
<a name="line866"></a>goog.net.BrowserChannel.setEndThreadExecutionHook = function(endHook) {
<a name="line867"></a>  goog.net.BrowserChannel.endExecutionHook_ = endHook;
<a name="line868"></a>};
<a name="line869"></a>
<a name="line870"></a>
<a name="line871"></a>/**
<a name="line872"></a> * Application provided execution hook for the start hook.
<a name="line873"></a> *
<a name="line874"></a> * @type {Function}
<a name="line875"></a> * @private
<a name="line876"></a> */
<a name="line877"></a>goog.net.BrowserChannel.startExecutionHook_ = function() { };
<a name="line878"></a>
<a name="line879"></a>
<a name="line880"></a>/**
<a name="line881"></a> * Application provided execution hook for the end hook.
<a name="line882"></a> *
<a name="line883"></a> * @type {Function}
<a name="line884"></a> * @private
<a name="line885"></a> */
<a name="line886"></a>goog.net.BrowserChannel.endExecutionHook_ = function() { };
<a name="line887"></a>
<a name="line888"></a>
<a name="line889"></a>/**
<a name="line890"></a> * Instantiates a ChannelRequest with the given parameters. Overidden in tests.
<a name="line891"></a> *
<a name="line892"></a> * @param {goog.net.BrowserChannel|goog.net.BrowserTestChannel} channel
<a name="line893"></a> *     The BrowserChannel that owns this request.
<a name="line894"></a> * @param {goog.net.ChannelDebug} channelDebug A ChannelDebug to use for
<a name="line895"></a> *     logging.
<a name="line896"></a> * @param {string=} opt_sessionId  The session id for the channel.
<a name="line897"></a> * @param {string|number=} opt_requestId  The request id for this request.
<a name="line898"></a> * @param {number=} opt_retryId  The retry id for this request.
<a name="line899"></a> * @return {goog.net.ChannelRequest} The created channel request.
<a name="line900"></a> */
<a name="line901"></a>goog.net.BrowserChannel.createChannelRequest = function(channel, channelDebug,
<a name="line902"></a>    opt_sessionId, opt_requestId, opt_retryId) {
<a name="line903"></a>  return new goog.net.ChannelRequest(
<a name="line904"></a>      channel,
<a name="line905"></a>      channelDebug,
<a name="line906"></a>      opt_sessionId,
<a name="line907"></a>      opt_requestId,
<a name="line908"></a>      opt_retryId);
<a name="line909"></a>};
<a name="line910"></a>
<a name="line911"></a>
<a name="line912"></a>/**
<a name="line913"></a> * Starts the channel. This initiates connections to the server.
<a name="line914"></a> *
<a name="line915"></a> * @param {string} testPath  The path for the test connection.
<a name="line916"></a> * @param {string} channelPath  The path for the channel connection.
<a name="line917"></a> * @param {Object=} opt_extraParams  Extra parameter keys and values to add to
<a name="line918"></a> *     the requests.
<a name="line919"></a> * @param {string=} opt_oldSessionId  Session ID from a previous session.
<a name="line920"></a> * @param {number=} opt_oldArrayId  The last array ID from a previous session.
<a name="line921"></a> */
<a name="line922"></a>goog.net.BrowserChannel.prototype.connect = function(testPath, channelPath,
<a name="line923"></a>    opt_extraParams, opt_oldSessionId, opt_oldArrayId) {
<a name="line924"></a>  this.channelDebug_.debug(&#39;connect()&#39;);
<a name="line925"></a>
<a name="line926"></a>  goog.net.BrowserChannel.notifyStatEvent(
<a name="line927"></a>      goog.net.BrowserChannel.Stat.CONNECT_ATTEMPT);
<a name="line928"></a>
<a name="line929"></a>  this.path_ = channelPath;
<a name="line930"></a>  this.extraParams_ = opt_extraParams || {};
<a name="line931"></a>
<a name="line932"></a>  // Attach parameters about the previous session if reconnecting.
<a name="line933"></a>  if (opt_oldSessionId &amp;&amp; goog.isDef(opt_oldArrayId)) {
<a name="line934"></a>    this.extraParams_[&#39;OSID&#39;] = opt_oldSessionId;
<a name="line935"></a>    this.extraParams_[&#39;OAID&#39;] = opt_oldArrayId;
<a name="line936"></a>  }
<a name="line937"></a>
<a name="line938"></a>  this.connectTest_(testPath);
<a name="line939"></a>};
<a name="line940"></a>
<a name="line941"></a>
<a name="line942"></a>/**
<a name="line943"></a> * Disconnects and closes the channel.
<a name="line944"></a> */
<a name="line945"></a>goog.net.BrowserChannel.prototype.disconnect = function() {
<a name="line946"></a>  this.channelDebug_.debug(&#39;disconnect()&#39;);
<a name="line947"></a>
<a name="line948"></a>  this.cancelRequests_();
<a name="line949"></a>
<a name="line950"></a>  if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line951"></a>    var rid = this.nextRid_++;
<a name="line952"></a>    var uri = this.forwardChannelUri_.clone();
<a name="line953"></a>    uri.setParameterValue(&#39;SID&#39;, this.sid_);
<a name="line954"></a>    uri.setParameterValue(&#39;RID&#39;, rid);
<a name="line955"></a>    uri.setParameterValue(&#39;TYPE&#39;, &#39;terminate&#39;);
<a name="line956"></a>
<a name="line957"></a>    // Add the reconnect parameters.
<a name="line958"></a>    this.addAdditionalParams_(uri);
<a name="line959"></a>
<a name="line960"></a>    var request = goog.net.BrowserChannel.createChannelRequest(
<a name="line961"></a>        this, this.channelDebug_, this.sid_, rid);
<a name="line962"></a>    request.sendUsingImgTag(uri);
<a name="line963"></a>  }
<a name="line964"></a>
<a name="line965"></a>  this.onClose_();
<a name="line966"></a>};
<a name="line967"></a>
<a name="line968"></a>
<a name="line969"></a>/**
<a name="line970"></a> * Returns the session id of the channel. Only available after the
<a name="line971"></a> * channel has been opened.
<a name="line972"></a> * @return {string} Session ID.
<a name="line973"></a> */
<a name="line974"></a>goog.net.BrowserChannel.prototype.getSessionId = function() {
<a name="line975"></a>  return this.sid_;
<a name="line976"></a>};
<a name="line977"></a>
<a name="line978"></a>
<a name="line979"></a>/**
<a name="line980"></a> * Starts the test channel to determine network conditions.
<a name="line981"></a> *
<a name="line982"></a> * @param {string} testPath  The relative PATH for the test connection.
<a name="line983"></a> * @private
<a name="line984"></a> */
<a name="line985"></a>goog.net.BrowserChannel.prototype.connectTest_ = function(testPath) {
<a name="line986"></a>  this.channelDebug_.debug(&#39;connectTest_()&#39;);
<a name="line987"></a>  if (!this.okToMakeRequest_()) {
<a name="line988"></a>    return; // channel is cancelled
<a name="line989"></a>  }
<a name="line990"></a>  this.connectionTest_ = new goog.net.BrowserTestChannel(
<a name="line991"></a>      this, this.channelDebug_);
<a name="line992"></a>  this.connectionTest_.setExtraHeaders(this.extraHeaders_);
<a name="line993"></a>  this.connectionTest_.setParser(this.parser_);
<a name="line994"></a>  this.connectionTest_.connect(testPath);
<a name="line995"></a>};
<a name="line996"></a>
<a name="line997"></a>
<a name="line998"></a>/**
<a name="line999"></a> * Starts the regular channel which is run after the test channel is complete.
<a name="line1000"></a> * @private
<a name="line1001"></a> */
<a name="line1002"></a>goog.net.BrowserChannel.prototype.connectChannel_ = function() {
<a name="line1003"></a>  this.channelDebug_.debug(&#39;connectChannel_()&#39;);
<a name="line1004"></a>  this.ensureInState_(goog.net.BrowserChannel.State.INIT,
<a name="line1005"></a>      goog.net.BrowserChannel.State.CLOSED);
<a name="line1006"></a>  this.forwardChannelUri_ =
<a name="line1007"></a>      this.getForwardChannelUri(/** @type {string} */ (this.path_));
<a name="line1008"></a>  this.ensureForwardChannel_();
<a name="line1009"></a>};
<a name="line1010"></a>
<a name="line1011"></a>
<a name="line1012"></a>/**
<a name="line1013"></a> * Cancels all outstanding requests.
<a name="line1014"></a> * @private
<a name="line1015"></a> */
<a name="line1016"></a>goog.net.BrowserChannel.prototype.cancelRequests_ = function() {
<a name="line1017"></a>  if (this.connectionTest_) {
<a name="line1018"></a>    this.connectionTest_.abort();
<a name="line1019"></a>    this.connectionTest_ = null;
<a name="line1020"></a>  }
<a name="line1021"></a>
<a name="line1022"></a>  if (this.backChannelRequest_) {
<a name="line1023"></a>    this.backChannelRequest_.cancel();
<a name="line1024"></a>    this.backChannelRequest_ = null;
<a name="line1025"></a>  }
<a name="line1026"></a>
<a name="line1027"></a>  if (this.backChannelTimerId_) {
<a name="line1028"></a>    goog.global.clearTimeout(this.backChannelTimerId_);
<a name="line1029"></a>    this.backChannelTimerId_ = null;
<a name="line1030"></a>  }
<a name="line1031"></a>
<a name="line1032"></a>  this.clearDeadBackchannelTimer_();
<a name="line1033"></a>
<a name="line1034"></a>  if (this.forwardChannelRequest_) {
<a name="line1035"></a>    this.forwardChannelRequest_.cancel();
<a name="line1036"></a>    this.forwardChannelRequest_ = null;
<a name="line1037"></a>  }
<a name="line1038"></a>
<a name="line1039"></a>  if (this.forwardChannelTimerId_) {
<a name="line1040"></a>    goog.global.clearTimeout(this.forwardChannelTimerId_);
<a name="line1041"></a>    this.forwardChannelTimerId_ = null;
<a name="line1042"></a>  }
<a name="line1043"></a>};
<a name="line1044"></a>
<a name="line1045"></a>
<a name="line1046"></a>/**
<a name="line1047"></a> * Returns the extra HTTP headers to add to all the requests sent to the server.
<a name="line1048"></a> *
<a name="line1049"></a> * @return {Object} The HTTP headers, or null.
<a name="line1050"></a> */
<a name="line1051"></a>goog.net.BrowserChannel.prototype.getExtraHeaders = function() {
<a name="line1052"></a>  return this.extraHeaders_;
<a name="line1053"></a>};
<a name="line1054"></a>
<a name="line1055"></a>
<a name="line1056"></a>/**
<a name="line1057"></a> * Sets extra HTTP headers to add to all the requests sent to the server.
<a name="line1058"></a> *
<a name="line1059"></a> * @param {Object} extraHeaders The HTTP headers, or null.
<a name="line1060"></a> */
<a name="line1061"></a>goog.net.BrowserChannel.prototype.setExtraHeaders = function(extraHeaders) {
<a name="line1062"></a>  this.extraHeaders_ = extraHeaders;
<a name="line1063"></a>};
<a name="line1064"></a>
<a name="line1065"></a>
<a name="line1066"></a>/**
<a name="line1067"></a> * Sets the throttle for handling onreadystatechange events for the request.
<a name="line1068"></a> *
<a name="line1069"></a> * @param {number} throttle The throttle in ms.  A value of zero indicates
<a name="line1070"></a> *     no throttle.
<a name="line1071"></a> */
<a name="line1072"></a>goog.net.BrowserChannel.prototype.setReadyStateChangeThrottle = function(
<a name="line1073"></a>    throttle) {
<a name="line1074"></a>  this.readyStateChangeThrottleMs_ = throttle;
<a name="line1075"></a>};
<a name="line1076"></a>
<a name="line1077"></a>
<a name="line1078"></a>/**
<a name="line1079"></a> * Sets whether cross origin requests are supported for the browser channel.
<a name="line1080"></a> *
<a name="line1081"></a> * Setting this allows the creation of requests to secondary domains and
<a name="line1082"></a> * sends XHRs with the CORS withCredentials bit set to true.
<a name="line1083"></a> *
<a name="line1084"></a> * In order for cross-origin requests to work, the server will also need to set
<a name="line1085"></a> * CORS response headers as per:
<a name="line1086"></a> * https://developer.mozilla.org/en-US/docs/HTTP_access_control
<a name="line1087"></a> *
<a name="line1088"></a> * See {@link goog.net.XhrIo#setWithCredentials}.
<a name="line1089"></a> * @param {boolean} supportCrossDomain Whether cross domain XHRs are supported.
<a name="line1090"></a> */
<a name="line1091"></a>goog.net.BrowserChannel.prototype.setSupportsCrossDomainXhrs = function(
<a name="line1092"></a>    supportCrossDomain) {
<a name="line1093"></a>  this.supportsCrossDomainXhrs_ = supportCrossDomain;
<a name="line1094"></a>};
<a name="line1095"></a>
<a name="line1096"></a>
<a name="line1097"></a>/**
<a name="line1098"></a> * Returns the handler used for channel callback events.
<a name="line1099"></a> *
<a name="line1100"></a> * @return {goog.net.BrowserChannel.Handler} The handler.
<a name="line1101"></a> */
<a name="line1102"></a>goog.net.BrowserChannel.prototype.getHandler = function() {
<a name="line1103"></a>  return this.handler_;
<a name="line1104"></a>};
<a name="line1105"></a>
<a name="line1106"></a>
<a name="line1107"></a>/**
<a name="line1108"></a> * Sets the handler used for channel callback events.
<a name="line1109"></a> * @param {goog.net.BrowserChannel.Handler} handler The handler to set.
<a name="line1110"></a> */
<a name="line1111"></a>goog.net.BrowserChannel.prototype.setHandler = function(handler) {
<a name="line1112"></a>  this.handler_ = handler;
<a name="line1113"></a>};
<a name="line1114"></a>
<a name="line1115"></a>
<a name="line1116"></a>/**
<a name="line1117"></a> * Returns whether the channel allows the use of a subdomain. There may be
<a name="line1118"></a> * cases where this isn&#39;t allowed.
<a name="line1119"></a> * @return {boolean} Whether a host prefix is allowed.
<a name="line1120"></a> */
<a name="line1121"></a>goog.net.BrowserChannel.prototype.getAllowHostPrefix = function() {
<a name="line1122"></a>  return this.allowHostPrefix_;
<a name="line1123"></a>};
<a name="line1124"></a>
<a name="line1125"></a>
<a name="line1126"></a>/**
<a name="line1127"></a> * Sets whether the channel allows the use of a subdomain. There may be cases
<a name="line1128"></a> * where this isn&#39;t allowed, for example, logging in with troutboard where
<a name="line1129"></a> * using a subdomain causes Apache to force the user to authenticate twice.
<a name="line1130"></a> * @param {boolean} allowHostPrefix Whether a host prefix is allowed.
<a name="line1131"></a> */
<a name="line1132"></a>goog.net.BrowserChannel.prototype.setAllowHostPrefix =
<a name="line1133"></a>    function(allowHostPrefix) {
<a name="line1134"></a>  this.allowHostPrefix_ = allowHostPrefix;
<a name="line1135"></a>};
<a name="line1136"></a>
<a name="line1137"></a>
<a name="line1138"></a>/**
<a name="line1139"></a> * Returns whether the channel is buffered or not. This state is valid for
<a name="line1140"></a> * querying only after the test connection has completed. This may be
<a name="line1141"></a> * queried in the goog.net.BrowserChannel.okToMakeRequest() callback.
<a name="line1142"></a> * A channel may be buffered if the test connection determines that
<a name="line1143"></a> * a chunked response could not be sent down within a suitable time.
<a name="line1144"></a> * @return {boolean} Whether the channel is buffered.
<a name="line1145"></a> */
<a name="line1146"></a>goog.net.BrowserChannel.prototype.isBuffered = function() {
<a name="line1147"></a>  return !this.useChunked_;
<a name="line1148"></a>};
<a name="line1149"></a>
<a name="line1150"></a>
<a name="line1151"></a>/**
<a name="line1152"></a> * Returns whether chunked mode is allowed. In certain debugging situations,
<a name="line1153"></a> * it&#39;s useful for the application to have a way to disable chunked mode for a
<a name="line1154"></a> * user.
<a name="line1155"></a>
<a name="line1156"></a> * @return {boolean} Whether chunked mode is allowed.
<a name="line1157"></a> */
<a name="line1158"></a>goog.net.BrowserChannel.prototype.getAllowChunkedMode =
<a name="line1159"></a>    function() {
<a name="line1160"></a>  return this.allowChunkedMode_;
<a name="line1161"></a>};
<a name="line1162"></a>
<a name="line1163"></a>
<a name="line1164"></a>/**
<a name="line1165"></a> * Sets whether chunked mode is allowed. In certain debugging situations, it&#39;s
<a name="line1166"></a> * useful for the application to have a way to disable chunked mode for a user.
<a name="line1167"></a> * @param {boolean} allowChunkedMode  Whether chunked mode is allowed.
<a name="line1168"></a> */
<a name="line1169"></a>goog.net.BrowserChannel.prototype.setAllowChunkedMode =
<a name="line1170"></a>    function(allowChunkedMode) {
<a name="line1171"></a>  this.allowChunkedMode_ = allowChunkedMode;
<a name="line1172"></a>};
<a name="line1173"></a>
<a name="line1174"></a>
<a name="line1175"></a>/**
<a name="line1176"></a> * Sends a request to the server. The format of the request is a Map data
<a name="line1177"></a> * structure of key/value pairs. These maps are then encoded in a format
<a name="line1178"></a> * suitable for the wire and then reconstituted as a Map data structure that
<a name="line1179"></a> * the server can process.
<a name="line1180"></a> * @param {Object|goog.structs.Map} map  The map to send.
<a name="line1181"></a> * @param {?Object=} opt_context The context associated with the map.
<a name="line1182"></a> */
<a name="line1183"></a>goog.net.BrowserChannel.prototype.sendMap = function(map, opt_context) {
<a name="line1184"></a>  if (this.state_ == goog.net.BrowserChannel.State.CLOSED) {
<a name="line1185"></a>    throw Error(&#39;Invalid operation: sending map when state is closed&#39;);
<a name="line1186"></a>  }
<a name="line1187"></a>
<a name="line1188"></a>  // We can only send 1000 maps per POST, but typically we should never have
<a name="line1189"></a>  // that much to send, so warn if we exceed that (we still send all the maps).
<a name="line1190"></a>  if (this.outgoingMaps_.length ==
<a name="line1191"></a>      goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_) {
<a name="line1192"></a>    // severe() is temporary so that we get these uploaded and can figure out
<a name="line1193"></a>    // what&#39;s causing them. Afterwards can change to warning().
<a name="line1194"></a>    this.channelDebug_.severe(
<a name="line1195"></a>        &#39;Already have &#39; + goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_ +
<a name="line1196"></a>        &#39; queued maps upon queueing &#39; + goog.json.serialize(map));
<a name="line1197"></a>  }
<a name="line1198"></a>
<a name="line1199"></a>  this.outgoingMaps_.push(
<a name="line1200"></a>      new goog.net.BrowserChannel.QueuedMap(this.nextMapId_++, map,
<a name="line1201"></a>                                            opt_context));
<a name="line1202"></a>  if (this.state_ == goog.net.BrowserChannel.State.OPENING ||
<a name="line1203"></a>      this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line1204"></a>    this.ensureForwardChannel_();
<a name="line1205"></a>  }
<a name="line1206"></a>};
<a name="line1207"></a>
<a name="line1208"></a>
<a name="line1209"></a>/**
<a name="line1210"></a> * When set to true, this changes the behavior of the forward channel so it
<a name="line1211"></a> * will not retry requests; it will fail after one network failure, and if
<a name="line1212"></a> * there was already one network failure, the request will fail immediately.
<a name="line1213"></a> * @param {boolean} failFast  Whether or not to fail fast.
<a name="line1214"></a> */
<a name="line1215"></a>goog.net.BrowserChannel.prototype.setFailFast = function(failFast) {
<a name="line1216"></a>  this.failFast_ = failFast;
<a name="line1217"></a>  this.channelDebug_.info(&#39;setFailFast: &#39; + failFast);
<a name="line1218"></a>  if ((this.forwardChannelRequest_ || this.forwardChannelTimerId_) &amp;&amp;
<a name="line1219"></a>      this.forwardChannelRetryCount_ &gt; this.getForwardChannelMaxRetries()) {
<a name="line1220"></a>    this.channelDebug_.info(
<a name="line1221"></a>        &#39;Retry count &#39; + this.forwardChannelRetryCount_ +
<a name="line1222"></a>        &#39; &gt; new maxRetries &#39; + this.getForwardChannelMaxRetries() +
<a name="line1223"></a>        &#39;. Fail immediately!&#39;);
<a name="line1224"></a>    if (this.forwardChannelRequest_) {
<a name="line1225"></a>      this.forwardChannelRequest_.cancel();
<a name="line1226"></a>      // Go through the standard onRequestComplete logic to expose the max-retry
<a name="line1227"></a>      // failure in the standard way.
<a name="line1228"></a>      this.onRequestComplete(this.forwardChannelRequest_);
<a name="line1229"></a>    } else {  // i.e., this.forwardChannelTimerId_
<a name="line1230"></a>      goog.global.clearTimeout(this.forwardChannelTimerId_);
<a name="line1231"></a>      this.forwardChannelTimerId_ = null;
<a name="line1232"></a>      // The error code from the last failed request is gone, so just use a
<a name="line1233"></a>      // generic one.
<a name="line1234"></a>      this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);
<a name="line1235"></a>    }
<a name="line1236"></a>  }
<a name="line1237"></a>};
<a name="line1238"></a>
<a name="line1239"></a>
<a name="line1240"></a>/**
<a name="line1241"></a> * @return {number} The max number of forward-channel retries, which will be 0
<a name="line1242"></a> * in fail-fast mode.
<a name="line1243"></a> */
<a name="line1244"></a>goog.net.BrowserChannel.prototype.getForwardChannelMaxRetries = function() {
<a name="line1245"></a>  return this.failFast_ ? 0 : this.forwardChannelMaxRetries_;
<a name="line1246"></a>};
<a name="line1247"></a>
<a name="line1248"></a>
<a name="line1249"></a>/**
<a name="line1250"></a> * Sets the maximum number of attempts to connect to the server for forward
<a name="line1251"></a> * channel requests.
<a name="line1252"></a> * @param {number} retries The maximum number of attempts.
<a name="line1253"></a> */
<a name="line1254"></a>goog.net.BrowserChannel.prototype.setForwardChannelMaxRetries =
<a name="line1255"></a>    function(retries) {
<a name="line1256"></a>  this.forwardChannelMaxRetries_ = retries;
<a name="line1257"></a>};
<a name="line1258"></a>
<a name="line1259"></a>
<a name="line1260"></a>/**
<a name="line1261"></a> * Sets the timeout for a forward channel request.
<a name="line1262"></a> * @param {number} timeoutMs The timeout in milliseconds.
<a name="line1263"></a> */
<a name="line1264"></a>goog.net.BrowserChannel.prototype.setForwardChannelRequestTimeout =
<a name="line1265"></a>    function(timeoutMs) {
<a name="line1266"></a>  this.forwardChannelRequestTimeoutMs_ = timeoutMs;
<a name="line1267"></a>};
<a name="line1268"></a>
<a name="line1269"></a>
<a name="line1270"></a>/**
<a name="line1271"></a> * @return {number} The max number of back-channel retries, which is a constant.
<a name="line1272"></a> */
<a name="line1273"></a>goog.net.BrowserChannel.prototype.getBackChannelMaxRetries = function() {
<a name="line1274"></a>  // Back-channel retries is a constant.
<a name="line1275"></a>  return goog.net.BrowserChannel.BACK_CHANNEL_MAX_RETRIES;
<a name="line1276"></a>};
<a name="line1277"></a>
<a name="line1278"></a>
<a name="line1279"></a>/**
<a name="line1280"></a> * Returns whether the channel is closed
<a name="line1281"></a> * @return {boolean} true if the channel is closed.
<a name="line1282"></a> */
<a name="line1283"></a>goog.net.BrowserChannel.prototype.isClosed = function() {
<a name="line1284"></a>  return this.state_ == goog.net.BrowserChannel.State.CLOSED;
<a name="line1285"></a>};
<a name="line1286"></a>
<a name="line1287"></a>
<a name="line1288"></a>/**
<a name="line1289"></a> * Returns the browser channel state.
<a name="line1290"></a> * @return {goog.net.BrowserChannel.State} The current state of the browser
<a name="line1291"></a> * channel.
<a name="line1292"></a> */
<a name="line1293"></a>goog.net.BrowserChannel.prototype.getState = function() {
<a name="line1294"></a>  return this.state_;
<a name="line1295"></a>};
<a name="line1296"></a>
<a name="line1297"></a>
<a name="line1298"></a>/**
<a name="line1299"></a> * Return the last status code received for a request.
<a name="line1300"></a> * @return {number} The last status code received for a request.
<a name="line1301"></a> */
<a name="line1302"></a>goog.net.BrowserChannel.prototype.getLastStatusCode = function() {
<a name="line1303"></a>  return this.lastStatusCode_;
<a name="line1304"></a>};
<a name="line1305"></a>
<a name="line1306"></a>
<a name="line1307"></a>/**
<a name="line1308"></a> * @return {number} The last array id received.
<a name="line1309"></a> */
<a name="line1310"></a>goog.net.BrowserChannel.prototype.getLastArrayId = function() {
<a name="line1311"></a>  return this.lastArrayId_;
<a name="line1312"></a>};
<a name="line1313"></a>
<a name="line1314"></a>
<a name="line1315"></a>/**
<a name="line1316"></a> * Returns whether there are outstanding requests servicing the channel.
<a name="line1317"></a> * @return {boolean} true if there are outstanding requests.
<a name="line1318"></a> */
<a name="line1319"></a>goog.net.BrowserChannel.prototype.hasOutstandingRequests = function() {
<a name="line1320"></a>  return this.outstandingRequests_() != 0;
<a name="line1321"></a>};
<a name="line1322"></a>
<a name="line1323"></a>
<a name="line1324"></a>/**
<a name="line1325"></a> * Sets a new parser for the response payload. A custom parser may be set to
<a name="line1326"></a> * avoid using eval(), for example. By default, the parser uses
<a name="line1327"></a> * {@code goog.json.unsafeParse}.
<a name="line1328"></a> * @param {!goog.string.Parser} parser Parser.
<a name="line1329"></a> */
<a name="line1330"></a>goog.net.BrowserChannel.prototype.setParser = function(parser) {
<a name="line1331"></a>  this.parser_ = parser;
<a name="line1332"></a>};
<a name="line1333"></a>
<a name="line1334"></a>
<a name="line1335"></a>/**
<a name="line1336"></a> * Returns the number of outstanding requests.
<a name="line1337"></a> * @return {number} The number of outstanding requests to the server.
<a name="line1338"></a> * @private
<a name="line1339"></a> */
<a name="line1340"></a>goog.net.BrowserChannel.prototype.outstandingRequests_ = function() {
<a name="line1341"></a>  var count = 0;
<a name="line1342"></a>  if (this.backChannelRequest_) {
<a name="line1343"></a>    count++;
<a name="line1344"></a>  }
<a name="line1345"></a>  if (this.forwardChannelRequest_) {
<a name="line1346"></a>    count++;
<a name="line1347"></a>  }
<a name="line1348"></a>  return count;
<a name="line1349"></a>};
<a name="line1350"></a>
<a name="line1351"></a>
<a name="line1352"></a>/**
<a name="line1353"></a> * Ensures that a forward channel request is scheduled.
<a name="line1354"></a> * @private
<a name="line1355"></a> */
<a name="line1356"></a>goog.net.BrowserChannel.prototype.ensureForwardChannel_ = function() {
<a name="line1357"></a>  if (this.forwardChannelRequest_) {
<a name="line1358"></a>    // connection in process - no need to start a new request
<a name="line1359"></a>    return;
<a name="line1360"></a>  }
<a name="line1361"></a>
<a name="line1362"></a>  if (this.forwardChannelTimerId_) {
<a name="line1363"></a>    // no need to start a new request - one is already scheduled
<a name="line1364"></a>    return;
<a name="line1365"></a>  }
<a name="line1366"></a>
<a name="line1367"></a>  this.forwardChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
<a name="line1368"></a>      goog.bind(this.onStartForwardChannelTimer_, this), 0);
<a name="line1369"></a>  this.forwardChannelRetryCount_ = 0;
<a name="line1370"></a>};
<a name="line1371"></a>
<a name="line1372"></a>
<a name="line1373"></a>/**
<a name="line1374"></a> * Schedules a forward-channel retry for the specified request, unless the max
<a name="line1375"></a> * retries has been reached.
<a name="line1376"></a> * @param {goog.net.ChannelRequest} request The failed request to retry.
<a name="line1377"></a> * @return {boolean} true iff a retry was scheduled.
<a name="line1378"></a> * @private
<a name="line1379"></a> */
<a name="line1380"></a>goog.net.BrowserChannel.prototype.maybeRetryForwardChannel_ =
<a name="line1381"></a>    function(request) {
<a name="line1382"></a>  if (this.forwardChannelRequest_ || this.forwardChannelTimerId_) {
<a name="line1383"></a>    // Should be impossible to be called in this state.
<a name="line1384"></a>    this.channelDebug_.severe(&#39;Request already in progress&#39;);
<a name="line1385"></a>    return false;
<a name="line1386"></a>  }
<a name="line1387"></a>
<a name="line1388"></a>  if (this.state_ == goog.net.BrowserChannel.State.INIT ||  // no retry open_()
<a name="line1389"></a>      (this.forwardChannelRetryCount_ &gt;= this.getForwardChannelMaxRetries())) {
<a name="line1390"></a>    return false;
<a name="line1391"></a>  }
<a name="line1392"></a>
<a name="line1393"></a>  this.channelDebug_.debug(&#39;Going to retry POST&#39;);
<a name="line1394"></a>
<a name="line1395"></a>  this.forwardChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
<a name="line1396"></a>      goog.bind(this.onStartForwardChannelTimer_, this, request),
<a name="line1397"></a>      this.getRetryTime_(this.forwardChannelRetryCount_));
<a name="line1398"></a>  this.forwardChannelRetryCount_++;
<a name="line1399"></a>  return true;
<a name="line1400"></a>};
<a name="line1401"></a>
<a name="line1402"></a>
<a name="line1403"></a>/**
<a name="line1404"></a> * Timer callback for ensureForwardChannel
<a name="line1405"></a> * @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry.
<a name="line1406"></a> * @private
<a name="line1407"></a> */
<a name="line1408"></a>goog.net.BrowserChannel.prototype.onStartForwardChannelTimer_ = function(
<a name="line1409"></a>    opt_retryRequest) {
<a name="line1410"></a>  this.forwardChannelTimerId_ = null;
<a name="line1411"></a>  this.startForwardChannel_(opt_retryRequest);
<a name="line1412"></a>};
<a name="line1413"></a>
<a name="line1414"></a>
<a name="line1415"></a>/**
<a name="line1416"></a> * Begins a new forward channel operation to the server.
<a name="line1417"></a> * @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry.
<a name="line1418"></a> * @private
<a name="line1419"></a> */
<a name="line1420"></a>goog.net.BrowserChannel.prototype.startForwardChannel_ = function(
<a name="line1421"></a>    opt_retryRequest) {
<a name="line1422"></a>  this.channelDebug_.debug(&#39;startForwardChannel_&#39;);
<a name="line1423"></a>  if (!this.okToMakeRequest_()) {
<a name="line1424"></a>    return; // channel is cancelled
<a name="line1425"></a>  } else if (this.state_ == goog.net.BrowserChannel.State.INIT) {
<a name="line1426"></a>    if (opt_retryRequest) {
<a name="line1427"></a>      this.channelDebug_.severe(&#39;Not supposed to retry the open&#39;);
<a name="line1428"></a>      return;
<a name="line1429"></a>    }
<a name="line1430"></a>    this.open_();
<a name="line1431"></a>    this.state_ = goog.net.BrowserChannel.State.OPENING;
<a name="line1432"></a>  } else if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line1433"></a>    if (opt_retryRequest) {
<a name="line1434"></a>      this.makeForwardChannelRequest_(opt_retryRequest);
<a name="line1435"></a>      return;
<a name="line1436"></a>    }
<a name="line1437"></a>
<a name="line1438"></a>    if (this.outgoingMaps_.length == 0) {
<a name="line1439"></a>      this.channelDebug_.debug(&#39;startForwardChannel_ returned: &#39; +
<a name="line1440"></a>                                   &#39;nothing to send&#39;);
<a name="line1441"></a>      // no need to start a new forward channel request
<a name="line1442"></a>      return;
<a name="line1443"></a>    }
<a name="line1444"></a>
<a name="line1445"></a>    if (this.forwardChannelRequest_) {
<a name="line1446"></a>      // Should be impossible to be called in this state.
<a name="line1447"></a>      this.channelDebug_.severe(&#39;startForwardChannel_ returned: &#39; +
<a name="line1448"></a>                                    &#39;connection already in progress&#39;);
<a name="line1449"></a>      return;
<a name="line1450"></a>    }
<a name="line1451"></a>
<a name="line1452"></a>    this.makeForwardChannelRequest_();
<a name="line1453"></a>    this.channelDebug_.debug(&#39;startForwardChannel_ finished, sent request&#39;);
<a name="line1454"></a>  }
<a name="line1455"></a>};
<a name="line1456"></a>
<a name="line1457"></a>
<a name="line1458"></a>/**
<a name="line1459"></a> * Establishes a new channel session with the the server.
<a name="line1460"></a> * @private
<a name="line1461"></a> */
<a name="line1462"></a>goog.net.BrowserChannel.prototype.open_ = function() {
<a name="line1463"></a>  this.channelDebug_.debug(&#39;open_()&#39;);
<a name="line1464"></a>  this.nextRid_ = Math.floor(Math.random() * 100000);
<a name="line1465"></a>
<a name="line1466"></a>  var rid = this.nextRid_++;
<a name="line1467"></a>  var request = goog.net.BrowserChannel.createChannelRequest(
<a name="line1468"></a>      this, this.channelDebug_, &#39;&#39;, rid);
<a name="line1469"></a>  request.setExtraHeaders(this.extraHeaders_);
<a name="line1470"></a>  var requestText = this.dequeueOutgoingMaps_();
<a name="line1471"></a>  var uri = this.forwardChannelUri_.clone();
<a name="line1472"></a>  uri.setParameterValue(&#39;RID&#39;, rid);
<a name="line1473"></a>  if (this.clientVersion_) {
<a name="line1474"></a>    uri.setParameterValue(&#39;CVER&#39;, this.clientVersion_);
<a name="line1475"></a>  }
<a name="line1476"></a>
<a name="line1477"></a>  // Add the reconnect parameters.
<a name="line1478"></a>  this.addAdditionalParams_(uri);
<a name="line1479"></a>
<a name="line1480"></a>  request.xmlHttpPost(uri, requestText, true);
<a name="line1481"></a>  this.forwardChannelRequest_ = request;
<a name="line1482"></a>};
<a name="line1483"></a>
<a name="line1484"></a>
<a name="line1485"></a>/**
<a name="line1486"></a> * Makes a forward channel request using XMLHTTP.
<a name="line1487"></a> * @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry.
<a name="line1488"></a> * @private
<a name="line1489"></a> */
<a name="line1490"></a>goog.net.BrowserChannel.prototype.makeForwardChannelRequest_ =
<a name="line1491"></a>    function(opt_retryRequest) {
<a name="line1492"></a>  var rid;
<a name="line1493"></a>  var requestText;
<a name="line1494"></a>  if (opt_retryRequest) {
<a name="line1495"></a>    if (this.channelVersion_ &gt; 6) {
<a name="line1496"></a>      // In version 7 and up we can tack on new arrays to a retry.
<a name="line1497"></a>      this.requeuePendingMaps_();
<a name="line1498"></a>      rid = this.nextRid_ - 1;  // Must use last RID
<a name="line1499"></a>      requestText = this.dequeueOutgoingMaps_();
<a name="line1500"></a>    } else {
<a name="line1501"></a>      // TODO(user): Remove this code and the opt_retryRequest passing
<a name="line1502"></a>      // once server-side support for ver 7 is ubiquitous.
<a name="line1503"></a>      rid = opt_retryRequest.getRequestId();
<a name="line1504"></a>      requestText = /** @type {string} */ (opt_retryRequest.getPostData());
<a name="line1505"></a>    }
<a name="line1506"></a>  } else {
<a name="line1507"></a>    rid = this.nextRid_++;
<a name="line1508"></a>    requestText = this.dequeueOutgoingMaps_();
<a name="line1509"></a>  }
<a name="line1510"></a>
<a name="line1511"></a>  var uri = this.forwardChannelUri_.clone();
<a name="line1512"></a>  uri.setParameterValue(&#39;SID&#39;, this.sid_);
<a name="line1513"></a>  uri.setParameterValue(&#39;RID&#39;, rid);
<a name="line1514"></a>  uri.setParameterValue(&#39;AID&#39;, this.lastArrayId_);
<a name="line1515"></a>  // Add the additional reconnect parameters.
<a name="line1516"></a>  this.addAdditionalParams_(uri);
<a name="line1517"></a>
<a name="line1518"></a>  var request = goog.net.BrowserChannel.createChannelRequest(
<a name="line1519"></a>      this,
<a name="line1520"></a>      this.channelDebug_,
<a name="line1521"></a>      this.sid_,
<a name="line1522"></a>      rid,
<a name="line1523"></a>      this.forwardChannelRetryCount_ + 1);
<a name="line1524"></a>  request.setExtraHeaders(this.extraHeaders_);
<a name="line1525"></a>
<a name="line1526"></a>  // randomize from 50%-100% of the forward channel timeout to avoid
<a name="line1527"></a>  // a big hit if servers happen to die at once.
<a name="line1528"></a>  request.setTimeout(
<a name="line1529"></a>      Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50) +
<a name="line1530"></a>      Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50 * Math.random()));
<a name="line1531"></a>  this.forwardChannelRequest_ = request;
<a name="line1532"></a>  request.xmlHttpPost(uri, requestText, true);
<a name="line1533"></a>};
<a name="line1534"></a>
<a name="line1535"></a>
<a name="line1536"></a>/**
<a name="line1537"></a> * Adds the additional parameters from the handler to the given URI.
<a name="line1538"></a> * @param {goog.Uri} uri The URI to add the parameters to.
<a name="line1539"></a> * @private
<a name="line1540"></a> */
<a name="line1541"></a>goog.net.BrowserChannel.prototype.addAdditionalParams_ = function(uri) {
<a name="line1542"></a>  // Add the additional reconnect parameters as needed.
<a name="line1543"></a>  if (this.handler_) {
<a name="line1544"></a>    var params = this.handler_.getAdditionalParams(this);
<a name="line1545"></a>    if (params) {
<a name="line1546"></a>      goog.structs.forEach(params, function(value, key, coll) {
<a name="line1547"></a>        uri.setParameterValue(key, value);
<a name="line1548"></a>      });
<a name="line1549"></a>    }
<a name="line1550"></a>  }
<a name="line1551"></a>};
<a name="line1552"></a>
<a name="line1553"></a>
<a name="line1554"></a>/**
<a name="line1555"></a> * Returns the request text from the outgoing maps and resets it.
<a name="line1556"></a> * @return {string} The encoded request text created from all the currently
<a name="line1557"></a> *                  queued outgoing maps.
<a name="line1558"></a> * @private
<a name="line1559"></a> */
<a name="line1560"></a>goog.net.BrowserChannel.prototype.dequeueOutgoingMaps_ = function() {
<a name="line1561"></a>  var count = Math.min(this.outgoingMaps_.length,
<a name="line1562"></a>                       goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_);
<a name="line1563"></a>  var sb = [&#39;count=&#39; + count];
<a name="line1564"></a>  var offset;
<a name="line1565"></a>  if (this.channelVersion_ &gt; 6 &amp;&amp; count &gt; 0) {
<a name="line1566"></a>    // To save a bit of bandwidth, specify the base mapId and the rest as
<a name="line1567"></a>    // offsets from it.
<a name="line1568"></a>    offset = this.outgoingMaps_[0].mapId;
<a name="line1569"></a>    sb.push(&#39;ofs=&#39; + offset);
<a name="line1570"></a>  } else {
<a name="line1571"></a>    offset = 0;
<a name="line1572"></a>  }
<a name="line1573"></a>  for (var i = 0; i &lt; count; i++) {
<a name="line1574"></a>    var mapId = this.outgoingMaps_[i].mapId;
<a name="line1575"></a>    var map = this.outgoingMaps_[i].map;
<a name="line1576"></a>    if (this.channelVersion_ &lt;= 6) {
<a name="line1577"></a>      // Map IDs were not used in ver 6 and before, just indexes in the request.
<a name="line1578"></a>      mapId = i;
<a name="line1579"></a>    } else {
<a name="line1580"></a>      mapId -= offset;
<a name="line1581"></a>    }
<a name="line1582"></a>    try {
<a name="line1583"></a>      goog.structs.forEach(map, function(value, key, coll) {
<a name="line1584"></a>        sb.push(&#39;req&#39; + mapId + &#39;_&#39; + key + &#39;=&#39; + encodeURIComponent(value));
<a name="line1585"></a>      });
<a name="line1586"></a>    } catch (ex) {
<a name="line1587"></a>      // We send a map here because lots of the retry logic relies on map IDs,
<a name="line1588"></a>      // so we have to send something.
<a name="line1589"></a>      sb.push(&#39;req&#39; + mapId + &#39;_&#39; + &#39;type&#39; + &#39;=&#39; +
<a name="line1590"></a>              encodeURIComponent(&#39;_badmap&#39;));
<a name="line1591"></a>      if (this.handler_) {
<a name="line1592"></a>        this.handler_.badMapError(this, map);
<a name="line1593"></a>      }
<a name="line1594"></a>    }
<a name="line1595"></a>  }
<a name="line1596"></a>  this.pendingMaps_ = this.pendingMaps_.concat(
<a name="line1597"></a>      this.outgoingMaps_.splice(0, count));
<a name="line1598"></a>  return sb.join(&#39;&amp;&#39;);
<a name="line1599"></a>};
<a name="line1600"></a>
<a name="line1601"></a>
<a name="line1602"></a>/**
<a name="line1603"></a> * Requeues unacknowledged sent arrays for retransmission in the next forward
<a name="line1604"></a> * channel request.
<a name="line1605"></a> * @private
<a name="line1606"></a> */
<a name="line1607"></a>goog.net.BrowserChannel.prototype.requeuePendingMaps_ = function() {
<a name="line1608"></a>  this.outgoingMaps_ = this.pendingMaps_.concat(this.outgoingMaps_);
<a name="line1609"></a>  this.pendingMaps_.length = 0;
<a name="line1610"></a>};
<a name="line1611"></a>
<a name="line1612"></a>
<a name="line1613"></a>/**
<a name="line1614"></a> * Ensures there is a backchannel request for receiving data from the server.
<a name="line1615"></a> * @private
<a name="line1616"></a> */
<a name="line1617"></a>goog.net.BrowserChannel.prototype.ensureBackChannel_ = function() {
<a name="line1618"></a>  if (this.backChannelRequest_) {
<a name="line1619"></a>    // already have one
<a name="line1620"></a>    return;
<a name="line1621"></a>  }
<a name="line1622"></a>
<a name="line1623"></a>  if (this.backChannelTimerId_) {
<a name="line1624"></a>    // no need to start a new request - one is already scheduled
<a name="line1625"></a>    return;
<a name="line1626"></a>  }
<a name="line1627"></a>
<a name="line1628"></a>  this.backChannelAttemptId_ = 1;
<a name="line1629"></a>  this.backChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
<a name="line1630"></a>      goog.bind(this.onStartBackChannelTimer_, this), 0);
<a name="line1631"></a>  this.backChannelRetryCount_ = 0;
<a name="line1632"></a>};
<a name="line1633"></a>
<a name="line1634"></a>
<a name="line1635"></a>/**
<a name="line1636"></a> * Schedules a back-channel retry, unless the max retries has been reached.
<a name="line1637"></a> * @return {boolean} true iff a retry was scheduled.
<a name="line1638"></a> * @private
<a name="line1639"></a> */
<a name="line1640"></a>goog.net.BrowserChannel.prototype.maybeRetryBackChannel_ = function() {
<a name="line1641"></a>  if (this.backChannelRequest_ || this.backChannelTimerId_) {
<a name="line1642"></a>    // Should be impossible to be called in this state.
<a name="line1643"></a>    this.channelDebug_.severe(&#39;Request already in progress&#39;);
<a name="line1644"></a>    return false;
<a name="line1645"></a>  }
<a name="line1646"></a>
<a name="line1647"></a>  if (this.backChannelRetryCount_ &gt;= this.getBackChannelMaxRetries()) {
<a name="line1648"></a>    return false;
<a name="line1649"></a>  }
<a name="line1650"></a>
<a name="line1651"></a>  this.channelDebug_.debug(&#39;Going to retry GET&#39;);
<a name="line1652"></a>
<a name="line1653"></a>  this.backChannelAttemptId_++;
<a name="line1654"></a>  this.backChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
<a name="line1655"></a>      goog.bind(this.onStartBackChannelTimer_, this),
<a name="line1656"></a>      this.getRetryTime_(this.backChannelRetryCount_));
<a name="line1657"></a>  this.backChannelRetryCount_++;
<a name="line1658"></a>  return true;
<a name="line1659"></a>};
<a name="line1660"></a>
<a name="line1661"></a>
<a name="line1662"></a>/**
<a name="line1663"></a> * Timer callback for ensureBackChannel_.
<a name="line1664"></a> * @private
<a name="line1665"></a> */
<a name="line1666"></a>goog.net.BrowserChannel.prototype.onStartBackChannelTimer_ = function() {
<a name="line1667"></a>  this.backChannelTimerId_ = null;
<a name="line1668"></a>  this.startBackChannel_();
<a name="line1669"></a>};
<a name="line1670"></a>
<a name="line1671"></a>
<a name="line1672"></a>/**
<a name="line1673"></a> * Begins a new back channel operation to the server.
<a name="line1674"></a> * @private
<a name="line1675"></a> */
<a name="line1676"></a>goog.net.BrowserChannel.prototype.startBackChannel_ = function() {
<a name="line1677"></a>  if (!this.okToMakeRequest_()) {
<a name="line1678"></a>    // channel is cancelled
<a name="line1679"></a>    return;
<a name="line1680"></a>  }
<a name="line1681"></a>
<a name="line1682"></a>  this.channelDebug_.debug(&#39;Creating new HttpRequest&#39;);
<a name="line1683"></a>  this.backChannelRequest_ = goog.net.BrowserChannel.createChannelRequest(
<a name="line1684"></a>      this,
<a name="line1685"></a>      this.channelDebug_,
<a name="line1686"></a>      this.sid_,
<a name="line1687"></a>      &#39;rpc&#39;,
<a name="line1688"></a>      this.backChannelAttemptId_);
<a name="line1689"></a>  this.backChannelRequest_.setExtraHeaders(this.extraHeaders_);
<a name="line1690"></a>  this.backChannelRequest_.setReadyStateChangeThrottle(
<a name="line1691"></a>      this.readyStateChangeThrottleMs_);
<a name="line1692"></a>  var uri = this.backChannelUri_.clone();
<a name="line1693"></a>  uri.setParameterValue(&#39;RID&#39;, &#39;rpc&#39;);
<a name="line1694"></a>  uri.setParameterValue(&#39;SID&#39;, this.sid_);
<a name="line1695"></a>  uri.setParameterValue(&#39;CI&#39;, this.useChunked_ ? &#39;0&#39; : &#39;1&#39;);
<a name="line1696"></a>  uri.setParameterValue(&#39;AID&#39;, this.lastArrayId_);
<a name="line1697"></a>
<a name="line1698"></a>  // Add the reconnect parameters.
<a name="line1699"></a>  this.addAdditionalParams_(uri);
<a name="line1700"></a>
<a name="line1701"></a>  if (!goog.net.ChannelRequest.supportsXhrStreaming()) {
<a name="line1702"></a>    uri.setParameterValue(&#39;TYPE&#39;, &#39;html&#39;);
<a name="line1703"></a>    this.backChannelRequest_.tridentGet(uri, Boolean(this.hostPrefix_));
<a name="line1704"></a>  } else {
<a name="line1705"></a>    uri.setParameterValue(&#39;TYPE&#39;, &#39;xmlhttp&#39;);
<a name="line1706"></a>    this.backChannelRequest_.xmlHttpGet(uri, true /* decodeChunks */,
<a name="line1707"></a>        this.hostPrefix_, false /* opt_noClose */);
<a name="line1708"></a>  }
<a name="line1709"></a>  this.channelDebug_.debug(&#39;New Request created&#39;);
<a name="line1710"></a>};
<a name="line1711"></a>
<a name="line1712"></a>
<a name="line1713"></a>/**
<a name="line1714"></a> * Gives the handler a chance to return an error code and stop channel
<a name="line1715"></a> * execution. A handler might want to do this to check that the user is still
<a name="line1716"></a> * logged in, for example.
<a name="line1717"></a> * @private
<a name="line1718"></a> * @return {boolean} If it&#39;s OK to make a request.
<a name="line1719"></a> */
<a name="line1720"></a>goog.net.BrowserChannel.prototype.okToMakeRequest_ = function() {
<a name="line1721"></a>  if (this.handler_) {
<a name="line1722"></a>    var result = this.handler_.okToMakeRequest(this);
<a name="line1723"></a>    if (result != goog.net.BrowserChannel.Error.OK) {
<a name="line1724"></a>      this.channelDebug_.debug(&#39;Handler returned error code from &#39; +
<a name="line1725"></a>                                   &#39;okToMakeRequest&#39;);
<a name="line1726"></a>      this.signalError_(result);
<a name="line1727"></a>      return false;
<a name="line1728"></a>    }
<a name="line1729"></a>  }
<a name="line1730"></a>  return true;
<a name="line1731"></a>};
<a name="line1732"></a>
<a name="line1733"></a>
<a name="line1734"></a>/**
<a name="line1735"></a> * Callback from BrowserTestChannel for when the channel is finished.
<a name="line1736"></a> * @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.
<a name="line1737"></a> * @param {boolean} useChunked  Whether we can chunk responses.
<a name="line1738"></a> */
<a name="line1739"></a>goog.net.BrowserChannel.prototype.testConnectionFinished =
<a name="line1740"></a>    function(testChannel, useChunked) {
<a name="line1741"></a>  this.channelDebug_.debug(&#39;Test Connection Finished&#39;);
<a name="line1742"></a>
<a name="line1743"></a>  this.useChunked_ = this.allowChunkedMode_ &amp;&amp; useChunked;
<a name="line1744"></a>  this.lastStatusCode_ = testChannel.getLastStatusCode();
<a name="line1745"></a>  this.connectChannel_();
<a name="line1746"></a>};
<a name="line1747"></a>
<a name="line1748"></a>
<a name="line1749"></a>/**
<a name="line1750"></a> * Callback from BrowserTestChannel for when the channel has an error.
<a name="line1751"></a> * @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.
<a name="line1752"></a> * @param {goog.net.ChannelRequest.Error} errorCode  The error code of the
<a name="line1753"></a>       failure.
<a name="line1754"></a> */
<a name="line1755"></a>goog.net.BrowserChannel.prototype.testConnectionFailure =
<a name="line1756"></a>    function(testChannel, errorCode) {
<a name="line1757"></a>  this.channelDebug_.debug(&#39;Test Connection Failed&#39;);
<a name="line1758"></a>  this.lastStatusCode_ = testChannel.getLastStatusCode();
<a name="line1759"></a>  this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);
<a name="line1760"></a>};
<a name="line1761"></a>
<a name="line1762"></a>
<a name="line1763"></a>/**
<a name="line1764"></a> * Callback from BrowserTestChannel for when the channel is blocked.
<a name="line1765"></a> * @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.
<a name="line1766"></a> */
<a name="line1767"></a>goog.net.BrowserChannel.prototype.testConnectionBlocked =
<a name="line1768"></a>    function(testChannel) {
<a name="line1769"></a>  this.channelDebug_.debug(&#39;Test Connection Blocked&#39;);
<a name="line1770"></a>  this.lastStatusCode_ = this.connectionTest_.getLastStatusCode();
<a name="line1771"></a>  this.signalError_(goog.net.BrowserChannel.Error.BLOCKED);
<a name="line1772"></a>};
<a name="line1773"></a>
<a name="line1774"></a>
<a name="line1775"></a>/**
<a name="line1776"></a> * Callback from ChannelRequest for when new data is received
<a name="line1777"></a> * @param {goog.net.ChannelRequest} request  The request object.
<a name="line1778"></a> * @param {string} responseText The text of the response.
<a name="line1779"></a> */
<a name="line1780"></a>goog.net.BrowserChannel.prototype.onRequestData =
<a name="line1781"></a>    function(request, responseText) {
<a name="line1782"></a>  if (this.state_ == goog.net.BrowserChannel.State.CLOSED ||
<a name="line1783"></a>      (this.backChannelRequest_ != request &amp;&amp;
<a name="line1784"></a>       this.forwardChannelRequest_ != request)) {
<a name="line1785"></a>    // either CLOSED or a request we don&#39;t know about (perhaps an old request)
<a name="line1786"></a>    return;
<a name="line1787"></a>  }
<a name="line1788"></a>  this.lastStatusCode_ = request.getLastStatusCode();
<a name="line1789"></a>
<a name="line1790"></a>  if (this.forwardChannelRequest_ == request &amp;&amp;
<a name="line1791"></a>      this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line1792"></a>    if (this.channelVersion_ &gt; 7) {
<a name="line1793"></a>      var response;
<a name="line1794"></a>      try {
<a name="line1795"></a>        response = this.parser_.parse(responseText);
<a name="line1796"></a>      } catch (ex) {
<a name="line1797"></a>        response = null;
<a name="line1798"></a>      }
<a name="line1799"></a>      if (goog.isArray(response) &amp;&amp; response.length == 3) {
<a name="line1800"></a>        this.handlePostResponse_(/** @type {Array} */ (response));
<a name="line1801"></a>      } else {
<a name="line1802"></a>        this.channelDebug_.debug(&#39;Bad POST response data returned&#39;);
<a name="line1803"></a>        this.signalError_(goog.net.BrowserChannel.Error.BAD_RESPONSE);
<a name="line1804"></a>      }
<a name="line1805"></a>    } else if (responseText != goog.net.BrowserChannel.MAGIC_RESPONSE_COOKIE) {
<a name="line1806"></a>      this.channelDebug_.debug(&#39;Bad data returned - missing/invald &#39; +
<a name="line1807"></a>                                   &#39;magic cookie&#39;);
<a name="line1808"></a>      this.signalError_(goog.net.BrowserChannel.Error.BAD_RESPONSE);
<a name="line1809"></a>    }
<a name="line1810"></a>  } else {
<a name="line1811"></a>    if (this.backChannelRequest_ == request) {
<a name="line1812"></a>      this.clearDeadBackchannelTimer_();
<a name="line1813"></a>    }
<a name="line1814"></a>    if (!goog.string.isEmpty(responseText)) {
<a name="line1815"></a>      var response = this.parser_.parse(responseText);
<a name="line1816"></a>      goog.asserts.assert(goog.isArray(response));
<a name="line1817"></a>      this.onInput_(/** @type {Array} */ (response));
<a name="line1818"></a>    }
<a name="line1819"></a>  }
<a name="line1820"></a>};
<a name="line1821"></a>
<a name="line1822"></a>
<a name="line1823"></a>/**
<a name="line1824"></a> * Handles a POST response from the server.
<a name="line1825"></a> * @param {Array} responseValues The key value pairs in the POST response.
<a name="line1826"></a> * @private
<a name="line1827"></a> */
<a name="line1828"></a>goog.net.BrowserChannel.prototype.handlePostResponse_ = function(
<a name="line1829"></a>    responseValues) {
<a name="line1830"></a>  // The first response value is set to 0 if server is missing backchannel.
<a name="line1831"></a>  if (responseValues[0] == 0) {
<a name="line1832"></a>    this.handleBackchannelMissing_();
<a name="line1833"></a>    return;
<a name="line1834"></a>  }
<a name="line1835"></a>  this.lastPostResponseArrayId_ = responseValues[1];
<a name="line1836"></a>  var outstandingArrays = this.lastPostResponseArrayId_ - this.lastArrayId_;
<a name="line1837"></a>  if (0 &lt; outstandingArrays) {
<a name="line1838"></a>    var numOutstandingBackchannelBytes = responseValues[2];
<a name="line1839"></a>    this.channelDebug_.debug(numOutstandingBackchannelBytes + &#39; bytes (in &#39; +
<a name="line1840"></a>        outstandingArrays + &#39; arrays) are outstanding on the BackChannel&#39;);
<a name="line1841"></a>    if (!this.shouldRetryBackChannel_(numOutstandingBackchannelBytes)) {
<a name="line1842"></a>      return;
<a name="line1843"></a>    }
<a name="line1844"></a>    if (!this.deadBackChannelTimerId_) {
<a name="line1845"></a>      // We expect to receive data within 2 RTTs or we retry the backchannel.
<a name="line1846"></a>      this.deadBackChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
<a name="line1847"></a>          goog.bind(this.onBackChannelDead_, this),
<a name="line1848"></a>          2 * goog.net.BrowserChannel.RTT_ESTIMATE);
<a name="line1849"></a>    }
<a name="line1850"></a>  }
<a name="line1851"></a>};
<a name="line1852"></a>
<a name="line1853"></a>
<a name="line1854"></a>/**
<a name="line1855"></a> * Handles a POST response from the server telling us that it has detected that
<a name="line1856"></a> * we have no hanging GET connection.
<a name="line1857"></a> * @private
<a name="line1858"></a> */
<a name="line1859"></a>goog.net.BrowserChannel.prototype.handleBackchannelMissing_ = function() {
<a name="line1860"></a>  // As long as the back channel was started before the POST was sent,
<a name="line1861"></a>  // we should retry the backchannel. We give a slight buffer of RTT_ESTIMATE
<a name="line1862"></a>  // so as not to excessively retry the backchannel
<a name="line1863"></a>  this.channelDebug_.debug(&#39;Server claims our backchannel is missing.&#39;);
<a name="line1864"></a>  if (this.backChannelTimerId_) {
<a name="line1865"></a>    this.channelDebug_.debug(&#39;But we are currently starting the request.&#39;);
<a name="line1866"></a>    return;
<a name="line1867"></a>  } else if (!this.backChannelRequest_) {
<a name="line1868"></a>    this.channelDebug_.warning(
<a name="line1869"></a>        &#39;We do not have a BackChannel established&#39;);
<a name="line1870"></a>  } else if (this.backChannelRequest_.getRequestStartTime() +
<a name="line1871"></a>      goog.net.BrowserChannel.RTT_ESTIMATE &lt;
<a name="line1872"></a>      this.forwardChannelRequest_.getRequestStartTime()) {
<a name="line1873"></a>    this.clearDeadBackchannelTimer_();
<a name="line1874"></a>    this.backChannelRequest_.cancel();
<a name="line1875"></a>    this.backChannelRequest_ = null;
<a name="line1876"></a>  } else {
<a name="line1877"></a>    return;
<a name="line1878"></a>  }
<a name="line1879"></a>  this.maybeRetryBackChannel_();
<a name="line1880"></a>  goog.net.BrowserChannel.notifyStatEvent(
<a name="line1881"></a>      goog.net.BrowserChannel.Stat.BACKCHANNEL_MISSING);
<a name="line1882"></a>};
<a name="line1883"></a>
<a name="line1884"></a>
<a name="line1885"></a>/**
<a name="line1886"></a> * Determines whether we should start the process of retrying a possibly
<a name="line1887"></a> * dead backchannel.
<a name="line1888"></a> * @param {number} outstandingBytes The number of bytes for which the server has
<a name="line1889"></a> *     not yet received acknowledgement.
<a name="line1890"></a> * @return {boolean} Whether to start the backchannel retry timer.
<a name="line1891"></a> * @private
<a name="line1892"></a> */
<a name="line1893"></a>goog.net.BrowserChannel.prototype.shouldRetryBackChannel_ = function(
<a name="line1894"></a>    outstandingBytes) {
<a name="line1895"></a>  // Not too many outstanding bytes, not buffered and not after a retry.
<a name="line1896"></a>  return outstandingBytes &lt;
<a name="line1897"></a>      goog.net.BrowserChannel.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF &amp;&amp;
<a name="line1898"></a>      !this.isBuffered() &amp;&amp;
<a name="line1899"></a>      this.backChannelRetryCount_ == 0;
<a name="line1900"></a>};
<a name="line1901"></a>
<a name="line1902"></a>
<a name="line1903"></a>/**
<a name="line1904"></a> * Decides which host prefix should be used, if any.  If there is a handler,
<a name="line1905"></a> * allows the handler to validate a host prefix provided by the server, and
<a name="line1906"></a> * optionally override it.
<a name="line1907"></a> * @param {?string} serverHostPrefix The host prefix provided by the server.
<a name="line1908"></a> * @return {?string} The host prefix to actually use, if any. Will return null
<a name="line1909"></a> *     if the use of host prefixes was disabled via setAllowHostPrefix().
<a name="line1910"></a> */
<a name="line1911"></a>goog.net.BrowserChannel.prototype.correctHostPrefix = function(
<a name="line1912"></a>    serverHostPrefix) {
<a name="line1913"></a>  if (this.allowHostPrefix_) {
<a name="line1914"></a>    if (this.handler_) {
<a name="line1915"></a>      return this.handler_.correctHostPrefix(serverHostPrefix);
<a name="line1916"></a>    }
<a name="line1917"></a>    return serverHostPrefix;
<a name="line1918"></a>  }
<a name="line1919"></a>  return null;
<a name="line1920"></a>};
<a name="line1921"></a>
<a name="line1922"></a>
<a name="line1923"></a>/**
<a name="line1924"></a> * Handles the timer that indicates that our backchannel is no longer able to
<a name="line1925"></a> * successfully receive data from the server.
<a name="line1926"></a> * @private
<a name="line1927"></a> */
<a name="line1928"></a>goog.net.BrowserChannel.prototype.onBackChannelDead_ = function() {
<a name="line1929"></a>  if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {
<a name="line1930"></a>    this.deadBackChannelTimerId_ = null;
<a name="line1931"></a>    this.backChannelRequest_.cancel();
<a name="line1932"></a>    this.backChannelRequest_ = null;
<a name="line1933"></a>    this.maybeRetryBackChannel_();
<a name="line1934"></a>    goog.net.BrowserChannel.notifyStatEvent(
<a name="line1935"></a>        goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD);
<a name="line1936"></a>  }
<a name="line1937"></a>};
<a name="line1938"></a>
<a name="line1939"></a>
<a name="line1940"></a>/**
<a name="line1941"></a> * Clears the timer that indicates that our backchannel is no longer able to
<a name="line1942"></a> * successfully receive data from the server.
<a name="line1943"></a> * @private
<a name="line1944"></a> */
<a name="line1945"></a>goog.net.BrowserChannel.prototype.clearDeadBackchannelTimer_ = function() {
<a name="line1946"></a>  if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {
<a name="line1947"></a>    goog.global.clearTimeout(this.deadBackChannelTimerId_);
<a name="line1948"></a>    this.deadBackChannelTimerId_ = null;
<a name="line1949"></a>  }
<a name="line1950"></a>};
<a name="line1951"></a>
<a name="line1952"></a>
<a name="line1953"></a>/**
<a name="line1954"></a> * Returns whether or not the given error/status combination is fatal or not.
<a name="line1955"></a> * On fatal errors we immediately close the session rather than retrying the
<a name="line1956"></a> * failed request.
<a name="line1957"></a> * @param {goog.net.ChannelRequest.Error?} error The error code for the failed
<a name="line1958"></a> * request.
<a name="line1959"></a> * @param {number} statusCode The last HTTP status code.
<a name="line1960"></a> * @return {boolean} Whether or not the error is fatal.
<a name="line1961"></a> * @private
<a name="line1962"></a> */
<a name="line1963"></a>goog.net.BrowserChannel.isFatalError_ =
<a name="line1964"></a>    function(error, statusCode) {
<a name="line1965"></a>  return error == goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID ||
<a name="line1966"></a>      error == goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED ||
<a name="line1967"></a>      (error == goog.net.ChannelRequest.Error.STATUS &amp;&amp;
<a name="line1968"></a>       statusCode &gt; 0);
<a name="line1969"></a>};
<a name="line1970"></a>
<a name="line1971"></a>
<a name="line1972"></a>/**
<a name="line1973"></a> * Callback from ChannelRequest that indicates a request has completed.
<a name="line1974"></a> * @param {goog.net.ChannelRequest} request  The request object.
<a name="line1975"></a> */
<a name="line1976"></a>goog.net.BrowserChannel.prototype.onRequestComplete =
<a name="line1977"></a>    function(request) {
<a name="line1978"></a>  this.channelDebug_.debug(&#39;Request complete&#39;);
<a name="line1979"></a>  var type;
<a name="line1980"></a>  if (this.backChannelRequest_ == request) {
<a name="line1981"></a>    this.clearDeadBackchannelTimer_();
<a name="line1982"></a>    this.backChannelRequest_ = null;
<a name="line1983"></a>    type = goog.net.BrowserChannel.ChannelType_.BACK_CHANNEL;
<a name="line1984"></a>  } else if (this.forwardChannelRequest_ == request) {
<a name="line1985"></a>    this.forwardChannelRequest_ = null;
<a name="line1986"></a>    type = goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL;
<a name="line1987"></a>  } else {
<a name="line1988"></a>    // return if it was an old request from a previous session
<a name="line1989"></a>    return;
<a name="line1990"></a>  }
<a name="line1991"></a>
<a name="line1992"></a>  this.lastStatusCode_ = request.getLastStatusCode();
<a name="line1993"></a>
<a name="line1994"></a>  if (this.state_ == goog.net.BrowserChannel.State.CLOSED) {
<a name="line1995"></a>    return;
<a name="line1996"></a>  }
<a name="line1997"></a>
<a name="line1998"></a>  if (request.getSuccess()) {
<a name="line1999"></a>    // Yay!
<a name="line2000"></a>    if (type == goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL) {
<a name="line2001"></a>      var size = request.getPostData() ? request.getPostData().length : 0;
<a name="line2002"></a>      goog.net.BrowserChannel.notifyTimingEvent(size,
<a name="line2003"></a>          goog.now() - request.getRequestStartTime(),
<a name="line2004"></a>          this.forwardChannelRetryCount_);
<a name="line2005"></a>      this.ensureForwardChannel_();
<a name="line2006"></a>      this.onSuccess_();
<a name="line2007"></a>      this.pendingMaps_.length = 0;
<a name="line2008"></a>    } else {  // i.e., back-channel
<a name="line2009"></a>      this.ensureBackChannel_();
<a name="line2010"></a>    }
<a name="line2011"></a>    return;
<a name="line2012"></a>  }
<a name="line2013"></a>  // Else unsuccessful. Fall through.
<a name="line2014"></a>
<a name="line2015"></a>  var lastError = request.getLastError();
<a name="line2016"></a>  if (!goog.net.BrowserChannel.isFatalError_(lastError,
<a name="line2017"></a>                                             this.lastStatusCode_)) {
<a name="line2018"></a>    // Maybe retry.
<a name="line2019"></a>    this.channelDebug_.debug(&#39;Maybe retrying, last error: &#39; +
<a name="line2020"></a>        goog.net.ChannelRequest.errorStringFromCode(
<a name="line2021"></a>            /** @type {goog.net.ChannelRequest.Error} */ (lastError),
<a name="line2022"></a>            this.lastStatusCode_));
<a name="line2023"></a>    if (type == goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL) {
<a name="line2024"></a>      if (this.maybeRetryForwardChannel_(request)) {
<a name="line2025"></a>        return;
<a name="line2026"></a>      }
<a name="line2027"></a>    }
<a name="line2028"></a>    if (type == goog.net.BrowserChannel.ChannelType_.BACK_CHANNEL) {
<a name="line2029"></a>      if (this.maybeRetryBackChannel_()) {
<a name="line2030"></a>        return;
<a name="line2031"></a>      }
<a name="line2032"></a>    }
<a name="line2033"></a>    // Else exceeded max retries. Fall through.
<a name="line2034"></a>    this.channelDebug_.debug(&#39;Exceeded max number of retries&#39;);
<a name="line2035"></a>  } else {
<a name="line2036"></a>    // Else fatal error. Fall through and mark the pending maps as failed.
<a name="line2037"></a>    this.channelDebug_.debug(&#39;Not retrying due to error type&#39;);
<a name="line2038"></a>  }
<a name="line2039"></a>
<a name="line2040"></a>
<a name="line2041"></a>  // Can&#39;t save this session. :(
<a name="line2042"></a>  this.channelDebug_.debug(&#39;Error: HTTP request failed&#39;);
<a name="line2043"></a>  switch (lastError) {
<a name="line2044"></a>    case goog.net.ChannelRequest.Error.NO_DATA:
<a name="line2045"></a>      this.signalError_(goog.net.BrowserChannel.Error.NO_DATA);
<a name="line2046"></a>      break;
<a name="line2047"></a>    case goog.net.ChannelRequest.Error.BAD_DATA:
<a name="line2048"></a>      this.signalError_(goog.net.BrowserChannel.Error.BAD_DATA);
<a name="line2049"></a>      break;
<a name="line2050"></a>    case goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID:
<a name="line2051"></a>      this.signalError_(goog.net.BrowserChannel.Error.UNKNOWN_SESSION_ID);
<a name="line2052"></a>      break;
<a name="line2053"></a>    case goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED:
<a name="line2054"></a>      this.signalError_(goog.net.BrowserChannel.Error.ACTIVE_X_BLOCKED);
<a name="line2055"></a>      break;
<a name="line2056"></a>    default:
<a name="line2057"></a>      this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);
<a name="line2058"></a>      break;
<a name="line2059"></a>  }
<a name="line2060"></a>};
<a name="line2061"></a>
<a name="line2062"></a>
<a name="line2063"></a>/**
<a name="line2064"></a> * @param {number} retryCount Number of retries so far.
<a name="line2065"></a> * @return {number} Time in ms before firing next retry request.
<a name="line2066"></a> * @private
<a name="line2067"></a> */
<a name="line2068"></a>goog.net.BrowserChannel.prototype.getRetryTime_ = function(retryCount) {
<a name="line2069"></a>  var retryTime = this.baseRetryDelayMs_ +
<a name="line2070"></a>      Math.floor(Math.random() * this.retryDelaySeedMs_);
<a name="line2071"></a>  if (!this.isActive()) {
<a name="line2072"></a>    this.channelDebug_.debug(&#39;Inactive channel&#39;);
<a name="line2073"></a>    retryTime =
<a name="line2074"></a>        retryTime * goog.net.BrowserChannel.INACTIVE_CHANNEL_RETRY_FACTOR;
<a name="line2075"></a>  }
<a name="line2076"></a>  // Backoff for subsequent retries
<a name="line2077"></a>  retryTime = retryTime * retryCount;
<a name="line2078"></a>  return retryTime;
<a name="line2079"></a>};
<a name="line2080"></a>
<a name="line2081"></a>
<a name="line2082"></a>/**
<a name="line2083"></a> * @param {number} baseDelayMs The base part of the retry delay, in ms.
<a name="line2084"></a> * @param {number} delaySeedMs A random delay between 0 and this is added to
<a name="line2085"></a> *     the base part.
<a name="line2086"></a> */
<a name="line2087"></a>goog.net.BrowserChannel.prototype.setRetryDelay = function(baseDelayMs,
<a name="line2088"></a>    delaySeedMs) {
<a name="line2089"></a>  this.baseRetryDelayMs_ = baseDelayMs;
<a name="line2090"></a>  this.retryDelaySeedMs_ = delaySeedMs;
<a name="line2091"></a>};
<a name="line2092"></a>
<a name="line2093"></a>
<a name="line2094"></a>/**
<a name="line2095"></a> * Processes the data returned by the server.
<a name="line2096"></a> * @param {Array} respArray The response array returned by the server.
<a name="line2097"></a> * @private
<a name="line2098"></a> */
<a name="line2099"></a>goog.net.BrowserChannel.prototype.onInput_ = function(respArray) {
<a name="line2100"></a>  // respArray is an array of arrays
<a name="line2101"></a>  var batch = this.handler_ &amp;&amp; this.handler_.channelHandleMultipleArrays ?
<a name="line2102"></a>      [] : null;
<a name="line2103"></a>  for (var i = 0; i &lt; respArray.length; i++) {
<a name="line2104"></a>    var nextArray = respArray[i];
<a name="line2105"></a>    this.lastArrayId_ = nextArray[0];
<a name="line2106"></a>    nextArray = nextArray[1];
<a name="line2107"></a>    if (this.state_ == goog.net.BrowserChannel.State.OPENING) {
<a name="line2108"></a>      if (nextArray[0] == &#39;c&#39;) {
<a name="line2109"></a>        this.sid_ = nextArray[1];
<a name="line2110"></a>        this.hostPrefix_ = this.correctHostPrefix(nextArray[2]);
<a name="line2111"></a>        var negotiatedVersion = nextArray[3];
<a name="line2112"></a>        if (goog.isDefAndNotNull(negotiatedVersion)) {
<a name="line2113"></a>          this.channelVersion_ = negotiatedVersion;
<a name="line2114"></a>        } else {
<a name="line2115"></a>          // Servers prior to version 7 did not send this, so assume version 6.
<a name="line2116"></a>          this.channelVersion_ = 6;
<a name="line2117"></a>        }
<a name="line2118"></a>        this.state_ = goog.net.BrowserChannel.State.OPENED;
<a name="line2119"></a>        if (this.handler_) {
<a name="line2120"></a>          this.handler_.channelOpened(this);
<a name="line2121"></a>        }
<a name="line2122"></a>        this.backChannelUri_ = this.getBackChannelUri(
<a name="line2123"></a>            this.hostPrefix_, /** @type {string} */ (this.path_));
<a name="line2124"></a>        // Open connection to receive data
<a name="line2125"></a>        this.ensureBackChannel_();
<a name="line2126"></a>      } else if (nextArray[0] == &#39;stop&#39;) {
<a name="line2127"></a>        this.signalError_(goog.net.BrowserChannel.Error.STOP);
<a name="line2128"></a>      }
<a name="line2129"></a>    } else if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line2130"></a>      if (nextArray[0] == &#39;stop&#39;) {
<a name="line2131"></a>        if (batch &amp;&amp; batch.length) {
<a name="line2132"></a>          this.handler_.channelHandleMultipleArrays(this, batch);
<a name="line2133"></a>          batch.length = 0;
<a name="line2134"></a>        }
<a name="line2135"></a>        this.signalError_(goog.net.BrowserChannel.Error.STOP);
<a name="line2136"></a>      } else if (nextArray[0] == &#39;noop&#39;) {
<a name="line2137"></a>        // ignore - noop to keep connection happy
<a name="line2138"></a>      } else {
<a name="line2139"></a>        if (batch) {
<a name="line2140"></a>          batch.push(nextArray);
<a name="line2141"></a>        } else if (this.handler_) {
<a name="line2142"></a>          this.handler_.channelHandleArray(this, nextArray);
<a name="line2143"></a>        }
<a name="line2144"></a>      }
<a name="line2145"></a>      // We have received useful data on the back-channel, so clear its retry
<a name="line2146"></a>      // count. We do this because back-channels by design do not complete
<a name="line2147"></a>      // quickly, so on a flaky connection we could have many fail to complete
<a name="line2148"></a>      // fully but still deliver a lot of data before they fail. We don&#39;t want
<a name="line2149"></a>      // to count such failures towards the retry limit, because we don&#39;t want
<a name="line2150"></a>      // to give up on a session if we can still receive data.
<a name="line2151"></a>      this.backChannelRetryCount_ = 0;
<a name="line2152"></a>    }
<a name="line2153"></a>  }
<a name="line2154"></a>  if (batch &amp;&amp; batch.length) {
<a name="line2155"></a>    this.handler_.channelHandleMultipleArrays(this, batch);
<a name="line2156"></a>  }
<a name="line2157"></a>};
<a name="line2158"></a>
<a name="line2159"></a>
<a name="line2160"></a>/**
<a name="line2161"></a> * Helper to ensure the BrowserChannel is in the expected state.
<a name="line2162"></a> * @param {...number} var_args The channel must be in one of the indicated
<a name="line2163"></a> *     states.
<a name="line2164"></a> * @private
<a name="line2165"></a> */
<a name="line2166"></a>goog.net.BrowserChannel.prototype.ensureInState_ = function(var_args) {
<a name="line2167"></a>  if (!goog.array.contains(arguments, this.state_)) {
<a name="line2168"></a>    throw Error(&#39;Unexpected channel state: &#39; + this.state_);
<a name="line2169"></a>  }
<a name="line2170"></a>};
<a name="line2171"></a>
<a name="line2172"></a>
<a name="line2173"></a>/**
<a name="line2174"></a> * Signals an error has occurred.
<a name="line2175"></a> * @param {goog.net.BrowserChannel.Error} error  The error code for the failure.
<a name="line2176"></a> * @private
<a name="line2177"></a> */
<a name="line2178"></a>goog.net.BrowserChannel.prototype.signalError_ = function(error) {
<a name="line2179"></a>  this.channelDebug_.info(&#39;Error code &#39; + error);
<a name="line2180"></a>  if (error == goog.net.BrowserChannel.Error.REQUEST_FAILED ||
<a name="line2181"></a>      error == goog.net.BrowserChannel.Error.BLOCKED) {
<a name="line2182"></a>    // Ping google to check if it&#39;s a server error or user&#39;s network error.
<a name="line2183"></a>    var imageUri = null;
<a name="line2184"></a>    if (this.handler_) {
<a name="line2185"></a>      imageUri = this.handler_.getNetworkTestImageUri(this);
<a name="line2186"></a>    }
<a name="line2187"></a>    goog.net.tmpnetwork.testGoogleCom(
<a name="line2188"></a>        goog.bind(this.testGoogleComCallback_, this), imageUri);
<a name="line2189"></a>  } else {
<a name="line2190"></a>    goog.net.BrowserChannel.notifyStatEvent(
<a name="line2191"></a>        goog.net.BrowserChannel.Stat.ERROR_OTHER);
<a name="line2192"></a>  }
<a name="line2193"></a>  this.onError_(error);
<a name="line2194"></a>};
<a name="line2195"></a>
<a name="line2196"></a>
<a name="line2197"></a>/**
<a name="line2198"></a> * Callback for testGoogleCom during error handling.
<a name="line2199"></a> * @param {boolean} networkUp Whether the network is up.
<a name="line2200"></a> * @private
<a name="line2201"></a> */
<a name="line2202"></a>goog.net.BrowserChannel.prototype.testGoogleComCallback_ = function(networkUp) {
<a name="line2203"></a>  if (networkUp) {
<a name="line2204"></a>    this.channelDebug_.info(&#39;Successfully pinged google.com&#39;);
<a name="line2205"></a>    goog.net.BrowserChannel.notifyStatEvent(
<a name="line2206"></a>        goog.net.BrowserChannel.Stat.ERROR_OTHER);
<a name="line2207"></a>  } else {
<a name="line2208"></a>    this.channelDebug_.info(&#39;Failed to ping google.com&#39;);
<a name="line2209"></a>    goog.net.BrowserChannel.notifyStatEvent(
<a name="line2210"></a>        goog.net.BrowserChannel.Stat.ERROR_NETWORK);
<a name="line2211"></a>    // We cann onError_ here instead of signalError_ because the latter just
<a name="line2212"></a>    // calls notifyStatEvent, and we don&#39;t want to have another stat event.
<a name="line2213"></a>    this.onError_(goog.net.BrowserChannel.Error.NETWORK);
<a name="line2214"></a>  }
<a name="line2215"></a>};
<a name="line2216"></a>
<a name="line2217"></a>
<a name="line2218"></a>/**
<a name="line2219"></a> * Called when messages have been successfully sent from the queue.
<a name="line2220"></a> * @private
<a name="line2221"></a> */
<a name="line2222"></a>goog.net.BrowserChannel.prototype.onSuccess_ = function() {
<a name="line2223"></a>  if (this.handler_) {
<a name="line2224"></a>    this.handler_.channelSuccess(this, this.pendingMaps_);
<a name="line2225"></a>  }
<a name="line2226"></a>};
<a name="line2227"></a>
<a name="line2228"></a>
<a name="line2229"></a>/**
<a name="line2230"></a> * Called when we&#39;ve determined the final error for a channel. It closes the
<a name="line2231"></a> * notifiers the handler of the error and closes the channel.
<a name="line2232"></a> * @param {goog.net.BrowserChannel.Error} error  The error code for the failure.
<a name="line2233"></a> * @private
<a name="line2234"></a> */
<a name="line2235"></a>goog.net.BrowserChannel.prototype.onError_ = function(error) {
<a name="line2236"></a>  this.channelDebug_.debug(&#39;HttpChannel: error - &#39; + error);
<a name="line2237"></a>  this.state_ = goog.net.BrowserChannel.State.CLOSED;
<a name="line2238"></a>  if (this.handler_) {
<a name="line2239"></a>    this.handler_.channelError(this, error);
<a name="line2240"></a>  }
<a name="line2241"></a>  this.onClose_();
<a name="line2242"></a>  this.cancelRequests_();
<a name="line2243"></a>};
<a name="line2244"></a>
<a name="line2245"></a>
<a name="line2246"></a>/**
<a name="line2247"></a> * Called when the channel has been closed. It notifiers the handler of the
<a name="line2248"></a> * event, and reports any pending or undelivered maps.
<a name="line2249"></a> * @private
<a name="line2250"></a> */
<a name="line2251"></a>goog.net.BrowserChannel.prototype.onClose_ = function() {
<a name="line2252"></a>  this.state_ = goog.net.BrowserChannel.State.CLOSED;
<a name="line2253"></a>  this.lastStatusCode_ = -1;
<a name="line2254"></a>  if (this.handler_) {
<a name="line2255"></a>    if (this.pendingMaps_.length == 0 &amp;&amp; this.outgoingMaps_.length == 0) {
<a name="line2256"></a>      this.handler_.channelClosed(this);
<a name="line2257"></a>    } else {
<a name="line2258"></a>      this.channelDebug_.debug(&#39;Number of undelivered maps&#39; +
<a name="line2259"></a>          &#39;, pending: &#39; + this.pendingMaps_.length +
<a name="line2260"></a>          &#39;, outgoing: &#39; + this.outgoingMaps_.length);
<a name="line2261"></a>
<a name="line2262"></a>      var copyOfPendingMaps = goog.array.clone(this.pendingMaps_);
<a name="line2263"></a>      var copyOfUndeliveredMaps = goog.array.clone(this.outgoingMaps_);
<a name="line2264"></a>      this.pendingMaps_.length = 0;
<a name="line2265"></a>      this.outgoingMaps_.length = 0;
<a name="line2266"></a>
<a name="line2267"></a>      this.handler_.channelClosed(this,
<a name="line2268"></a>          copyOfPendingMaps,
<a name="line2269"></a>          copyOfUndeliveredMaps);
<a name="line2270"></a>    }
<a name="line2271"></a>  }
<a name="line2272"></a>};
<a name="line2273"></a>
<a name="line2274"></a>
<a name="line2275"></a>/**
<a name="line2276"></a> * Gets the Uri used for the connection that sends data to the server.
<a name="line2277"></a> * @param {string} path The path on the host.
<a name="line2278"></a> * @return {goog.Uri} The forward channel URI.
<a name="line2279"></a> */
<a name="line2280"></a>goog.net.BrowserChannel.prototype.getForwardChannelUri =
<a name="line2281"></a>    function(path) {
<a name="line2282"></a>  var uri = this.createDataUri(null, path);
<a name="line2283"></a>  this.channelDebug_.debug(&#39;GetForwardChannelUri: &#39; + uri);
<a name="line2284"></a>  return uri;
<a name="line2285"></a>};
<a name="line2286"></a>
<a name="line2287"></a>
<a name="line2288"></a>/**
<a name="line2289"></a> * Gets the results for the first browser channel test
<a name="line2290"></a> * @return {Array.&lt;string&gt;} The results.
<a name="line2291"></a> */
<a name="line2292"></a>goog.net.BrowserChannel.prototype.getFirstTestResults =
<a name="line2293"></a>    function() {
<a name="line2294"></a>  return this.firstTestResults_;
<a name="line2295"></a>};
<a name="line2296"></a>
<a name="line2297"></a>
<a name="line2298"></a>/**
<a name="line2299"></a> * Gets the Uri used for the connection that receives data from the server.
<a name="line2300"></a> * @param {?string} hostPrefix The host prefix.
<a name="line2301"></a> * @param {string} path The path on the host.
<a name="line2302"></a> * @return {goog.Uri} The back channel URI.
<a name="line2303"></a> */
<a name="line2304"></a>goog.net.BrowserChannel.prototype.getBackChannelUri =
<a name="line2305"></a>    function(hostPrefix, path) {
<a name="line2306"></a>  var uri = this.createDataUri(this.shouldUseSecondaryDomains() ?
<a name="line2307"></a>      hostPrefix : null, path);
<a name="line2308"></a>  this.channelDebug_.debug(&#39;GetBackChannelUri: &#39; + uri);
<a name="line2309"></a>  return uri;
<a name="line2310"></a>};
<a name="line2311"></a>
<a name="line2312"></a>
<a name="line2313"></a>/**
<a name="line2314"></a> * Creates a data Uri applying logic for secondary hostprefix, port
<a name="line2315"></a> * overrides, and versioning.
<a name="line2316"></a> * @param {?string} hostPrefix The host prefix.
<a name="line2317"></a> * @param {string} path The path on the host (may be absolute or relative).
<a name="line2318"></a> * @param {number=} opt_overridePort Optional override port.
<a name="line2319"></a> * @return {goog.Uri} The data URI.
<a name="line2320"></a> */
<a name="line2321"></a>goog.net.BrowserChannel.prototype.createDataUri =
<a name="line2322"></a>    function(hostPrefix, path, opt_overridePort) {
<a name="line2323"></a>  var uri = goog.Uri.parse(path);
<a name="line2324"></a>  var uriAbsolute = (uri.getDomain() != &#39;&#39;);
<a name="line2325"></a>  if (uriAbsolute) {
<a name="line2326"></a>    if (hostPrefix) {
<a name="line2327"></a>      uri.setDomain(hostPrefix + &#39;.&#39; + uri.getDomain());
<a name="line2328"></a>    }
<a name="line2329"></a>
<a name="line2330"></a>    uri.setPort(opt_overridePort || uri.getPort());
<a name="line2331"></a>  } else {
<a name="line2332"></a>    var locationPage = window.location;
<a name="line2333"></a>    var hostName;
<a name="line2334"></a>    if (hostPrefix) {
<a name="line2335"></a>      hostName = hostPrefix + &#39;.&#39; + locationPage.hostname;
<a name="line2336"></a>    } else {
<a name="line2337"></a>      hostName = locationPage.hostname;
<a name="line2338"></a>    }
<a name="line2339"></a>
<a name="line2340"></a>    var port = opt_overridePort || locationPage.port;
<a name="line2341"></a>
<a name="line2342"></a>    uri = goog.Uri.create(locationPage.protocol, null, hostName, port, path);
<a name="line2343"></a>  }
<a name="line2344"></a>
<a name="line2345"></a>  if (this.extraParams_) {
<a name="line2346"></a>    goog.structs.forEach(this.extraParams_, function(value, key, coll) {
<a name="line2347"></a>      uri.setParameterValue(key, value);
<a name="line2348"></a>    });
<a name="line2349"></a>  }
<a name="line2350"></a>
<a name="line2351"></a>  // Add the protocol version to the URI.
<a name="line2352"></a>  uri.setParameterValue(&#39;VER&#39;, this.channelVersion_);
<a name="line2353"></a>
<a name="line2354"></a>  // Add the reconnect parameters.
<a name="line2355"></a>  this.addAdditionalParams_(uri);
<a name="line2356"></a>
<a name="line2357"></a>  return uri;
<a name="line2358"></a>};
<a name="line2359"></a>
<a name="line2360"></a>
<a name="line2361"></a>/**
<a name="line2362"></a> * Called when BC needs to create an XhrIo object.  Override in a subclass if
<a name="line2363"></a> * you need to customize the behavior, for example to enable the creation of
<a name="line2364"></a> * XHR&#39;s capable of calling a secondary domain. Will also allow calling
<a name="line2365"></a> * a secondary domain if withCredentials (CORS) is enabled.
<a name="line2366"></a> * @param {?string} hostPrefix The host prefix, if we need an XhrIo object
<a name="line2367"></a> *     capable of calling a secondary domain.
<a name="line2368"></a> * @return {!goog.net.XhrIo} A new XhrIo object.
<a name="line2369"></a> */
<a name="line2370"></a>goog.net.BrowserChannel.prototype.createXhrIo = function(hostPrefix) {
<a name="line2371"></a>  if (hostPrefix &amp;&amp; !this.supportsCrossDomainXhrs_) {
<a name="line2372"></a>    throw Error(&#39;Can\&#39;t create secondary domain capable XhrIo object.&#39;);
<a name="line2373"></a>  }
<a name="line2374"></a>  var xhr = new goog.net.XhrIo();
<a name="line2375"></a>  xhr.setWithCredentials(this.supportsCrossDomainXhrs_);
<a name="line2376"></a>  return xhr;
<a name="line2377"></a>};
<a name="line2378"></a>
<a name="line2379"></a>
<a name="line2380"></a>/**
<a name="line2381"></a> * Gets whether this channel is currently active. This is used to determine the
<a name="line2382"></a> * length of time to wait before retrying. This call delegates to the handler.
<a name="line2383"></a> * @return {boolean} Whether the channel is currently active.
<a name="line2384"></a> */
<a name="line2385"></a>goog.net.BrowserChannel.prototype.isActive = function() {
<a name="line2386"></a>  return !!this.handler_ &amp;&amp; this.handler_.isActive(this);
<a name="line2387"></a>};
<a name="line2388"></a>
<a name="line2389"></a>
<a name="line2390"></a>/**
<a name="line2391"></a> * Wrapper around SafeTimeout which calls the start and end execution hooks
<a name="line2392"></a> * with a try...finally block.
<a name="line2393"></a> * @param {Function} fn The callback function.
<a name="line2394"></a> * @param {number} ms The time in MS for the timer.
<a name="line2395"></a> * @return {number} The ID of the timer.
<a name="line2396"></a> */
<a name="line2397"></a>goog.net.BrowserChannel.setTimeout = function(fn, ms) {
<a name="line2398"></a>  if (!goog.isFunction(fn)) {
<a name="line2399"></a>    throw Error(&#39;Fn must not be null and must be a function&#39;);
<a name="line2400"></a>  }
<a name="line2401"></a>  return goog.global.setTimeout(function() {
<a name="line2402"></a>    goog.net.BrowserChannel.onStartExecution();
<a name="line2403"></a>    try {
<a name="line2404"></a>      fn();
<a name="line2405"></a>    } finally {
<a name="line2406"></a>      goog.net.BrowserChannel.onEndExecution();
<a name="line2407"></a>    }
<a name="line2408"></a>  }, ms);
<a name="line2409"></a>};
<a name="line2410"></a>
<a name="line2411"></a>
<a name="line2412"></a>/**
<a name="line2413"></a> * Helper function to call the start hook
<a name="line2414"></a> */
<a name="line2415"></a>goog.net.BrowserChannel.onStartExecution = function() {
<a name="line2416"></a>  goog.net.BrowserChannel.startExecutionHook_();
<a name="line2417"></a>};
<a name="line2418"></a>
<a name="line2419"></a>
<a name="line2420"></a>/**
<a name="line2421"></a> * Helper function to call the end hook
<a name="line2422"></a> */
<a name="line2423"></a>goog.net.BrowserChannel.onEndExecution = function() {
<a name="line2424"></a>  goog.net.BrowserChannel.endExecutionHook_();
<a name="line2425"></a>};
<a name="line2426"></a>
<a name="line2427"></a>
<a name="line2428"></a>/**
<a name="line2429"></a> * Returns the singleton event target for stat events.
<a name="line2430"></a> * @return {goog.events.EventTarget} The event target for stat events.
<a name="line2431"></a> */
<a name="line2432"></a>goog.net.BrowserChannel.getStatEventTarget = function() {
<a name="line2433"></a>  return goog.net.BrowserChannel.statEventTarget_;
<a name="line2434"></a>};
<a name="line2435"></a>
<a name="line2436"></a>
<a name="line2437"></a>/**
<a name="line2438"></a> * Notify the channel that a particular fine grained network event has occurred.
<a name="line2439"></a> * Should be considered package-private.
<a name="line2440"></a> * @param {goog.net.BrowserChannel.ServerReachability} reachabilityType The
<a name="line2441"></a> *     reachability event type.
<a name="line2442"></a> */
<a name="line2443"></a>goog.net.BrowserChannel.prototype.notifyServerReachabilityEvent = function(
<a name="line2444"></a>    reachabilityType) {
<a name="line2445"></a>  var target = goog.net.BrowserChannel.statEventTarget_;
<a name="line2446"></a>  target.dispatchEvent(new goog.net.BrowserChannel.ServerReachabilityEvent(
<a name="line2447"></a>      target, reachabilityType));
<a name="line2448"></a>};
<a name="line2449"></a>
<a name="line2450"></a>
<a name="line2451"></a>/**
<a name="line2452"></a> * Helper function to call the stat event callback.
<a name="line2453"></a> * @param {goog.net.BrowserChannel.Stat} stat The stat.
<a name="line2454"></a> */
<a name="line2455"></a>goog.net.BrowserChannel.notifyStatEvent = function(stat) {
<a name="line2456"></a>  var target = goog.net.BrowserChannel.statEventTarget_;
<a name="line2457"></a>  target.dispatchEvent(
<a name="line2458"></a>      new goog.net.BrowserChannel.StatEvent(target, stat));
<a name="line2459"></a>};
<a name="line2460"></a>
<a name="line2461"></a>
<a name="line2462"></a>/**
<a name="line2463"></a> * Helper function to notify listeners about POST request performance.
<a name="line2464"></a> *
<a name="line2465"></a> * @param {number} size Number of characters in the POST data.
<a name="line2466"></a> * @param {number} rtt The amount of time from POST start to response.
<a name="line2467"></a> * @param {number} retries The number of times the POST had to be retried.
<a name="line2468"></a> */
<a name="line2469"></a>goog.net.BrowserChannel.notifyTimingEvent = function(size, rtt, retries) {
<a name="line2470"></a>  var target = goog.net.BrowserChannel.statEventTarget_;
<a name="line2471"></a>  target.dispatchEvent(
<a name="line2472"></a>      new goog.net.BrowserChannel.TimingEvent(target, size, rtt, retries));
<a name="line2473"></a>};
<a name="line2474"></a>
<a name="line2475"></a>
<a name="line2476"></a>/**
<a name="line2477"></a> * Determines whether to use a secondary domain when the server gives us
<a name="line2478"></a> * a host prefix. This allows us to work around browser per-domain
<a name="line2479"></a> * connection limits.
<a name="line2480"></a> *
<a name="line2481"></a> * Currently, we  use secondary domains when using Trident&#39;s ActiveXObject,
<a name="line2482"></a> * because it supports cross-domain requests out of the box.  Note that in IE10
<a name="line2483"></a> * we no longer use ActiveX since it&#39;s not supported in Metro mode and IE10
<a name="line2484"></a> * supports XHR streaming.
<a name="line2485"></a> *
<a name="line2486"></a> * If you need to use secondary domains on other browsers and IE10,
<a name="line2487"></a> * you have two choices:
<a name="line2488"></a> *     1) If you only care about browsers that support CORS
<a name="line2489"></a> *        (https://developer.mozilla.org/en-US/docs/HTTP_access_control), you
<a name="line2490"></a> *        can use {@link #setSupportsCrossDomainXhrs} and set the appropriate
<a name="line2491"></a> *        CORS response headers on the server.
<a name="line2492"></a> *     2) Or, override this method in a subclass, and make sure that those
<a name="line2493"></a> *        browsers use some messaging mechanism that works cross-domain (e.g
<a name="line2494"></a> *        iframes and window.postMessage).
<a name="line2495"></a> *
<a name="line2496"></a> * @return {boolean} Whether to use secondary domains.
<a name="line2497"></a> * @see http://code.google.com/p/closure-library/issues/detail?id=339
<a name="line2498"></a> */
<a name="line2499"></a>goog.net.BrowserChannel.prototype.shouldUseSecondaryDomains = function() {
<a name="line2500"></a>  return this.supportsCrossDomainXhrs_ ||
<a name="line2501"></a>      !goog.net.ChannelRequest.supportsXhrStreaming();
<a name="line2502"></a>};
<a name="line2503"></a>
<a name="line2504"></a>
<a name="line2505"></a>/**
<a name="line2506"></a> * A LogSaver that can be used to accumulate all the debug logs for
<a name="line2507"></a> * BrowserChannels so they can be sent to the server when a problem is
<a name="line2508"></a> * detected.
<a name="line2509"></a> */
<a name="line2510"></a>goog.net.BrowserChannel.LogSaver = {};
<a name="line2511"></a>
<a name="line2512"></a>
<a name="line2513"></a>/**
<a name="line2514"></a> * Buffer for accumulating the debug log
<a name="line2515"></a> * @type {goog.structs.CircularBuffer}
<a name="line2516"></a> * @private
<a name="line2517"></a> */
<a name="line2518"></a>goog.net.BrowserChannel.LogSaver.buffer_ =
<a name="line2519"></a>    new goog.structs.CircularBuffer(1000);
<a name="line2520"></a>
<a name="line2521"></a>
<a name="line2522"></a>/**
<a name="line2523"></a> * Whether we&#39;re currently accumulating the debug log.
<a name="line2524"></a> * @type {boolean}
<a name="line2525"></a> * @private
<a name="line2526"></a> */
<a name="line2527"></a>goog.net.BrowserChannel.LogSaver.enabled_ = false;
<a name="line2528"></a>
<a name="line2529"></a>
<a name="line2530"></a>/**
<a name="line2531"></a> * Formatter for saving logs.
<a name="line2532"></a> * @type {goog.debug.Formatter}
<a name="line2533"></a> * @private
<a name="line2534"></a> */
<a name="line2535"></a>goog.net.BrowserChannel.LogSaver.formatter_ = new goog.debug.TextFormatter();
<a name="line2536"></a>
<a name="line2537"></a>
<a name="line2538"></a>/**
<a name="line2539"></a> * Returns whether the LogSaver is enabled.
<a name="line2540"></a> * @return {boolean} Whether saving is enabled or disabled.
<a name="line2541"></a> */
<a name="line2542"></a>goog.net.BrowserChannel.LogSaver.isEnabled = function() {
<a name="line2543"></a>  return goog.net.BrowserChannel.LogSaver.enabled_;
<a name="line2544"></a>};
<a name="line2545"></a>
<a name="line2546"></a>
<a name="line2547"></a>/**
<a name="line2548"></a> * Enables of disables the LogSaver.
<a name="line2549"></a> * @param {boolean} enable Whether to enable or disable saving.
<a name="line2550"></a> */
<a name="line2551"></a>goog.net.BrowserChannel.LogSaver.setEnabled = function(enable) {
<a name="line2552"></a>  if (enable == goog.net.BrowserChannel.LogSaver.enabled_) {
<a name="line2553"></a>    return;
<a name="line2554"></a>  }
<a name="line2555"></a>
<a name="line2556"></a>  var fn = goog.net.BrowserChannel.LogSaver.addLogRecord;
<a name="line2557"></a>  var logger = goog.debug.Logger.getLogger(&#39;goog.net&#39;);
<a name="line2558"></a>  if (enable) {
<a name="line2559"></a>    logger.addHandler(fn);
<a name="line2560"></a>  } else {
<a name="line2561"></a>    logger.removeHandler(fn);
<a name="line2562"></a>  }
<a name="line2563"></a>};
<a name="line2564"></a>
<a name="line2565"></a>
<a name="line2566"></a>/**
<a name="line2567"></a> * Adds a log record.
<a name="line2568"></a> * @param {goog.debug.LogRecord} logRecord the LogRecord.
<a name="line2569"></a> */
<a name="line2570"></a>goog.net.BrowserChannel.LogSaver.addLogRecord = function(logRecord) {
<a name="line2571"></a>  goog.net.BrowserChannel.LogSaver.buffer_.add(
<a name="line2572"></a>      goog.net.BrowserChannel.LogSaver.formatter_.formatRecord(logRecord));
<a name="line2573"></a>};
<a name="line2574"></a>
<a name="line2575"></a>
<a name="line2576"></a>/**
<a name="line2577"></a> * Returns the log as a single string.
<a name="line2578"></a> * @return {string} The log as a single string.
<a name="line2579"></a> */
<a name="line2580"></a>goog.net.BrowserChannel.LogSaver.getBuffer = function() {
<a name="line2581"></a>  return goog.net.BrowserChannel.LogSaver.buffer_.getValues().join(&#39;&#39;);
<a name="line2582"></a>};
<a name="line2583"></a>
<a name="line2584"></a>
<a name="line2585"></a>/**
<a name="line2586"></a> * Clears the buffer
<a name="line2587"></a> */
<a name="line2588"></a>goog.net.BrowserChannel.LogSaver.clearBuffer = function() {
<a name="line2589"></a>  goog.net.BrowserChannel.LogSaver.buffer_.clear();
<a name="line2590"></a>};
<a name="line2591"></a>
<a name="line2592"></a>
<a name="line2593"></a>
<a name="line2594"></a>/**
<a name="line2595"></a> * Interface for the browser channel handler
<a name="line2596"></a> * @constructor
<a name="line2597"></a> */
<a name="line2598"></a>goog.net.BrowserChannel.Handler = function() {
<a name="line2599"></a>};
<a name="line2600"></a>
<a name="line2601"></a>
<a name="line2602"></a>/**
<a name="line2603"></a> * Callback handler for when a batch of response arrays is received from the
<a name="line2604"></a> * server.
<a name="line2605"></a> * @type {Function}
<a name="line2606"></a> */
<a name="line2607"></a>goog.net.BrowserChannel.Handler.prototype.channelHandleMultipleArrays = null;
<a name="line2608"></a>
<a name="line2609"></a>
<a name="line2610"></a>/**
<a name="line2611"></a> * Whether it&#39;s okay to make a request to the server. A handler can return
<a name="line2612"></a> * false if the channel should fail. For example, if the user has logged out,
<a name="line2613"></a> * the handler may want all requests to fail immediately.
<a name="line2614"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2615"></a> * @return {goog.net.BrowserChannel.Error} An error code. The code should
<a name="line2616"></a> * return goog.net.BrowserChannel.Error.OK to indicate it&#39;s okay. Any other
<a name="line2617"></a> * error code will cause a failure.
<a name="line2618"></a> */
<a name="line2619"></a>goog.net.BrowserChannel.Handler.prototype.okToMakeRequest =
<a name="line2620"></a>    function(browserChannel) {
<a name="line2621"></a>  return goog.net.BrowserChannel.Error.OK;
<a name="line2622"></a>};
<a name="line2623"></a>
<a name="line2624"></a>
<a name="line2625"></a>/**
<a name="line2626"></a> * Indicates the BrowserChannel has successfully negotiated with the server
<a name="line2627"></a> * and can now send and receive data.
<a name="line2628"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2629"></a> */
<a name="line2630"></a>goog.net.BrowserChannel.Handler.prototype.channelOpened =
<a name="line2631"></a>    function(browserChannel) {
<a name="line2632"></a>};
<a name="line2633"></a>
<a name="line2634"></a>
<a name="line2635"></a>/**
<a name="line2636"></a> * New input is available for the application to process.
<a name="line2637"></a> *
<a name="line2638"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2639"></a> * @param {Array} array The data array.
<a name="line2640"></a> */
<a name="line2641"></a>goog.net.BrowserChannel.Handler.prototype.channelHandleArray =
<a name="line2642"></a>    function(browserChannel, array) {
<a name="line2643"></a>};
<a name="line2644"></a>
<a name="line2645"></a>
<a name="line2646"></a>/**
<a name="line2647"></a> * Indicates maps were successfully sent on the BrowserChannel.
<a name="line2648"></a> *
<a name="line2649"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2650"></a> * @param {Array.&lt;goog.net.BrowserChannel.QueuedMap&gt;} deliveredMaps The
<a name="line2651"></a> *     array of maps that have been delivered to the server. This is a direct
<a name="line2652"></a> *     reference to the internal BrowserChannel array, so a copy should be made
<a name="line2653"></a> *     if the caller desires a reference to the data.
<a name="line2654"></a> */
<a name="line2655"></a>goog.net.BrowserChannel.Handler.prototype.channelSuccess =
<a name="line2656"></a>    function(browserChannel, deliveredMaps) {
<a name="line2657"></a>};
<a name="line2658"></a>
<a name="line2659"></a>
<a name="line2660"></a>/**
<a name="line2661"></a> * Indicates an error occurred on the BrowserChannel.
<a name="line2662"></a> *
<a name="line2663"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2664"></a> * @param {goog.net.BrowserChannel.Error} error The error code.
<a name="line2665"></a> */
<a name="line2666"></a>goog.net.BrowserChannel.Handler.prototype.channelError =
<a name="line2667"></a>    function(browserChannel, error) {
<a name="line2668"></a>};
<a name="line2669"></a>
<a name="line2670"></a>
<a name="line2671"></a>/**
<a name="line2672"></a> * Indicates the BrowserChannel is closed. Also notifies about which maps,
<a name="line2673"></a> * if any, that may not have been delivered to the server.
<a name="line2674"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2675"></a> * @param {Array.&lt;goog.net.BrowserChannel.QueuedMap&gt;=} opt_pendingMaps The
<a name="line2676"></a> *     array of pending maps, which may or may not have been delivered to the
<a name="line2677"></a> *     server.
<a name="line2678"></a> * @param {Array.&lt;goog.net.BrowserChannel.QueuedMap&gt;=} opt_undeliveredMaps
<a name="line2679"></a> *     The array of undelivered maps, which have definitely not been delivered
<a name="line2680"></a> *     to the server.
<a name="line2681"></a> */
<a name="line2682"></a>goog.net.BrowserChannel.Handler.prototype.channelClosed =
<a name="line2683"></a>    function(browserChannel, opt_pendingMaps, opt_undeliveredMaps) {
<a name="line2684"></a>};
<a name="line2685"></a>
<a name="line2686"></a>
<a name="line2687"></a>/**
<a name="line2688"></a> * Gets any parameters that should be added at the time another connection is
<a name="line2689"></a> * made to the server.
<a name="line2690"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2691"></a> * @return {Object} Extra parameter keys and values to add to the
<a name="line2692"></a> *                  requests.
<a name="line2693"></a> */
<a name="line2694"></a>goog.net.BrowserChannel.Handler.prototype.getAdditionalParams =
<a name="line2695"></a>    function(browserChannel) {
<a name="line2696"></a>  return {};
<a name="line2697"></a>};
<a name="line2698"></a>
<a name="line2699"></a>
<a name="line2700"></a>/**
<a name="line2701"></a> * Gets the URI of an image that can be used to test network connectivity.
<a name="line2702"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2703"></a> * @return {goog.Uri?} A custom URI to load for the network test.
<a name="line2704"></a> */
<a name="line2705"></a>goog.net.BrowserChannel.Handler.prototype.getNetworkTestImageUri =
<a name="line2706"></a>    function(browserChannel) {
<a name="line2707"></a>  return null;
<a name="line2708"></a>};
<a name="line2709"></a>
<a name="line2710"></a>
<a name="line2711"></a>/**
<a name="line2712"></a> * Gets whether this channel is currently active. This is used to determine the
<a name="line2713"></a> * length of time to wait before retrying.
<a name="line2714"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2715"></a> * @return {boolean} Whether the channel is currently active.
<a name="line2716"></a> */
<a name="line2717"></a>goog.net.BrowserChannel.Handler.prototype.isActive = function(browserChannel) {
<a name="line2718"></a>  return true;
<a name="line2719"></a>};
<a name="line2720"></a>
<a name="line2721"></a>
<a name="line2722"></a>/**
<a name="line2723"></a> * Called by the channel if enumeration of the map throws an exception.
<a name="line2724"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line2725"></a> * @param {Object} map The map that can&#39;t be enumerated.
<a name="line2726"></a> */
<a name="line2727"></a>goog.net.BrowserChannel.Handler.prototype.badMapError =
<a name="line2728"></a>    function(browserChannel, map) {
<a name="line2729"></a>  return;
<a name="line2730"></a>};
<a name="line2731"></a>
<a name="line2732"></a>
<a name="line2733"></a>/**
<a name="line2734"></a> * Allows the handler to override a host prefix provided by the server.  Will
<a name="line2735"></a> * be called whenever the channel has received such a prefix and is considering
<a name="line2736"></a> * its use.
<a name="line2737"></a> * @param {?string} serverHostPrefix The host prefix provided by the server.
<a name="line2738"></a> * @return {?string} The host prefix the client should use.
<a name="line2739"></a> */
<a name="line2740"></a>goog.net.BrowserChannel.Handler.prototype.correctHostPrefix =
<a name="line2741"></a>    function(serverHostPrefix) {
<a name="line2742"></a>  return serverHostPrefix;
<a name="line2743"></a>};
</pre>


</body>
</html>
